Skip to content

Commit

Permalink
[web] Removes a few deprecated API usages. (#6177)
Browse files Browse the repository at this point in the history
This PR removes a bunch of deprecated APIs from the following packages:

* `web_benchmarks`
* `file_selector_web`
* `cross_file`
* `image_picker_platform_interface`

## Issues

* Fixes flutter/flutter#143113
* Fixes flutter/flutter#143399
* Fixes flutter/flutter#143878
* Fixes flutter/flutter#143892
* Closes #5248
  • Loading branch information
ditman authored Feb 22, 2024
1 parent 67470fc commit 7df2085
Show file tree
Hide file tree
Showing 17 changed files with 67 additions and 43 deletions.
4 changes: 4 additions & 0 deletions packages/cross_file/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.3.4+1

* Removes a few deprecated API usages.

## 0.3.4

* Updates to web code to package `web: ^0.5.0`.
Expand Down
36 changes: 20 additions & 16 deletions packages/cross_file/lib/src/types/html.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'dart:js_interop';
import 'dart:typed_data';

import 'package:meta/meta.dart';
import 'package:web/helpers.dart';
import 'package:web/web.dart';

import '../web_helpers/web_helpers.dart';
import 'base.dart';
Expand Down Expand Up @@ -133,22 +133,26 @@ class XFile extends XFileBase {
throw Exception('Safari cannot handle XFiles larger than 4GB.');
}

late XMLHttpRequest request;
try {
request = await HttpRequest.request(path, responseType: 'blob');
} on ProgressEvent catch (e) {
if (e.type == 'error') {
throw Exception(
'Could not load Blob from its URL. Has it been revoked?');
}
rethrow;
}

_browserBlob = request.response as Blob?;
final Completer<Blob> blobCompleter = Completer<Blob>();

assert(_browserBlob != null, 'The Blob backing this XFile cannot be null!');

return _browserBlob!;
late XMLHttpRequest request;
request = XMLHttpRequest()
..open('get', path, true)
..responseType = 'blob'
..onLoad.listen((ProgressEvent e) {
assert(request.response != null,
'The Blob backing this XFile cannot be null!');
blobCompleter.complete(request.response! as Blob);
})
..onError.listen((ProgressEvent e) {
if (e.type == 'error') {
blobCompleter.completeError(Exception(
'Could not load Blob from its URL. Has it been revoked?'));
}
})
..send();

return blobCompleter.future;
}

@override
Expand Down
6 changes: 3 additions & 3 deletions packages/cross_file/lib/src/web_helpers/web_helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:web/helpers.dart';
import 'package:web/web.dart';

/// Create anchor element with download attribute
HTMLAnchorElement createAnchorElement(String href, String? suggestedName) =>
Expand All @@ -20,11 +20,11 @@ void addElementToContainerAndClick(Element container, HTMLElement element) {

/// Initializes a DOM container where elements can be injected.
Element ensureInitialized(String id) {
Element? target = querySelector('#$id');
Element? target = document.querySelector('#$id');
if (target == null) {
final Element targetElement = document.createElement('flt-x-file')..id = id;

querySelector('body')!.appendChild(targetElement);
document.body!.appendChild(targetElement);
target = targetElement;
}
return target;
Expand Down
2 changes: 1 addition & 1 deletion packages/cross_file/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: cross_file
description: An abstraction to allow working with files across multiple platforms.
repository: https://github.com/flutter/packages/tree/main/packages/cross_file
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+cross_file%22
version: 0.3.4
version: 0.3.4+1

environment:
sdk: ^3.3.0
Expand Down
6 changes: 3 additions & 3 deletions packages/cross_file/test/x_file_html_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import 'dart:typed_data';

import 'package:cross_file/cross_file.dart';
import 'package:test/test.dart';
import 'package:web/helpers.dart' as html;
import 'package:web/web.dart' as html;

const String expectedStringContents = 'Hello, world! I ❤ ñ! 空手';
final Uint8List bytes = Uint8List.fromList(utf8.encode(expectedStringContents));
Expand Down Expand Up @@ -95,7 +95,7 @@ void main() {
await file.saveTo('');

final html.Element? container =
html.querySelector('#$crossFileDomElementId');
html.document.querySelector('#$crossFileDomElementId');

expect(container, isNotNull);
});
Expand All @@ -106,7 +106,7 @@ void main() {
await file.saveTo('path');

final html.Element container =
html.querySelector('#$crossFileDomElementId')!;
html.document.querySelector('#$crossFileDomElementId')!;

late html.HTMLAnchorElement element;
for (int i = 0; i < container.childNodes.length; i++) {
Expand Down
4 changes: 4 additions & 0 deletions packages/file_selector/file_selector_web/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.9.4+1

* Removes a few deprecated API usages.

## 0.9.4

* Updates web code to package `web: ^0.5.0`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'package:file_selector_platform_interface/file_selector_platform_interfac
import 'package:file_selector_web/src/dom_helper.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:web/helpers.dart';
import 'package:web/web.dart';

void main() {
group('dom_helper', () {
Expand Down Expand Up @@ -41,7 +41,8 @@ void main() {

setUp(() {
domHelper = DomHelper();
input = (createElementTag('input') as HTMLInputElement)..type = 'file';
input = (document.createElement('input') as HTMLInputElement)
..type = 'file';
});

group('getFiles', () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import 'package:file_selector_web/file_selector_web.dart';
import 'package:file_selector_web/src/dom_helper.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:web/helpers.dart';
import 'package:web/web.dart';

void main() {
group('FileSelectorWeb', () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'dart:js_interop';
import 'package:file_selector_platform_interface/file_selector_platform_interface.dart';
import 'package:flutter/foundation.dart' show visibleForTesting;
import 'package:flutter/services.dart';
import 'package:web/helpers.dart';
import 'package:web/web.dart';

/// Class to manipulate the DOM with the intention of reading files from it.
class DomHelper {
Expand Down
2 changes: 1 addition & 1 deletion packages/file_selector/file_selector_web/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: file_selector_web
description: Web platform implementation of file_selector
repository: https://github.com/flutter/packages/tree/main/packages/file_selector/file_selector_web
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+file_selector%22
version: 0.9.4
version: 0.9.4+1

environment:
sdk: ^3.3.0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## NEXT
## 2.9.4

* Updates minimum supported SDK version to Flutter 3.13/Dart 3.1.
* Updates minimum supported SDK version to Flutter 3.19/Dart 3.3.
* Removes a few deprecated API usages.

## 2.9.3

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class PickedFile extends PickedFileBase {

Future<Uint8List> get _bytes async {
if (_initBytes != null) {
return Future<Uint8List>.value(UnmodifiableUint8ListView(_initBytes!));
return _initBytes.asUnmodifiableView();
}
return http.readBytes(Uri.parse(path));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ repository: https://github.com/flutter/packages/tree/main/packages/image_picker/
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+image_picker%22
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
version: 2.9.3
version: 2.9.4

environment:
sdk: ^3.1.0
flutter: ">=3.13.0"
sdk: ^3.3.0
flutter: ">=3.19.0"

dependencies:
cross_file: ^0.3.1+1
Expand Down
4 changes: 4 additions & 0 deletions packages/web_benchmarks/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.2.1

* Removes a few deprecated API usages.

## 1.2.0

* Updates to web code to package `web: ^0.5.0`.
Expand Down
16 changes: 10 additions & 6 deletions packages/web_benchmarks/lib/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ class LocalBenchmarkServerClient {
/// DevTools Protocol.
Future<void> startPerformanceTracing(String? benchmarkName) async {
_checkNotManualMode();
await HttpRequest.request(
await _requestXhr(
'/start-performance-tracing?label=$benchmarkName',
method: 'POST',
mimeType: 'application/json',
Expand All @@ -328,7 +328,7 @@ class LocalBenchmarkServerClient {
/// Stops the performance tracing session started by [startPerformanceTracing].
Future<void> stopPerformanceTracing() async {
_checkNotManualMode();
await HttpRequest.request(
await _requestXhr(
'/stop-performance-tracing',
method: 'POST',
mimeType: 'application/json',
Expand Down Expand Up @@ -356,7 +356,7 @@ class LocalBenchmarkServerClient {
/// The server will halt the devicelab task and log the error.
Future<void> reportError(dynamic error, StackTrace stackTrace) async {
_checkNotManualMode();
await HttpRequest.request(
await _requestXhr(
'/on-error',
method: 'POST',
mimeType: 'application/json',
Expand All @@ -370,7 +370,7 @@ class LocalBenchmarkServerClient {
/// Reports a message about the demo to the benchmark server.
Future<void> printToConsole(String report) async {
_checkNotManualMode();
await HttpRequest.request(
await _requestXhr(
'/print-to-console',
method: 'POST',
mimeType: 'text/plain',
Expand All @@ -384,7 +384,7 @@ class LocalBenchmarkServerClient {
String url, {
required String method,
required String mimeType,
required String sendData,
String? sendData,
}) {
final Completer<XMLHttpRequest> completer = Completer<XMLHttpRequest>();
final XMLHttpRequest xhr = XMLHttpRequest();
Expand All @@ -394,7 +394,11 @@ class LocalBenchmarkServerClient {
completer.complete(xhr);
});
xhr.onError.listen(completer.completeError);
xhr.send(sendData.toJS);
if (sendData != null) {
xhr.send(sendData.toJS);
} else {
xhr.send();
}
return completer.future;
}
}
Expand Down
5 changes: 4 additions & 1 deletion packages/web_benchmarks/lib/src/recorder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -248,14 +248,17 @@ abstract class SceneBuilderRecorder extends Recorder {
}
};
PlatformDispatcher.instance.onDrawFrame = () {
final FlutterView? view = PlatformDispatcher.instance.implicitView;
try {
_profile.record('drawFrameDuration', () {
final SceneBuilder sceneBuilder = SceneBuilder();
onDrawFrame(sceneBuilder);
_profile.record('sceneBuildDuration', () {
final Scene scene = sceneBuilder.build();
_profile.record('windowRenderDuration', () {
window.render(scene);
assert(view != null,
'Cannot profile windowRenderDuration on a null View.');
view!.render(scene);
}, reported: false);
}, reported: false);
}, reported: true);
Expand Down
3 changes: 1 addition & 2 deletions packages/web_benchmarks/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: web_benchmarks
description: A benchmark harness for performance-testing Flutter apps in Chrome.
repository: https://github.com/flutter/packages/tree/main/packages/web_benchmarks
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+web_benchmarks%22
version: 1.2.0
version: 1.2.1

environment:
sdk: ^3.3.0
Expand All @@ -14,7 +14,6 @@ dependencies:
sdk: flutter
flutter_test:
sdk: flutter
http: ^1.0.0
logging: ^1.0.2
meta: ^1.7.0
path: ^1.8.0
Expand Down

0 comments on commit 7df2085

Please sign in to comment.