Skip to content

Commit c7b9a24

Browse files
authored
[image_picker_web] Migrate to null-safety (flutter#3535)
1 parent e026242 commit c7b9a24

File tree

4 files changed

+45
-41
lines changed

4 files changed

+45
-41
lines changed

packages/image_picker/image_picker_for_web/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 2.0.0-nullsafety
2+
3+
* Migrate to null safety.
4+
15
# 0.1.0+3
26

37
* Update Flutter SDK constraint.

packages/image_picker/image_picker_for_web/lib/image_picker_for_web.dart

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ final String _kAcceptVideoMimeType = 'video/3gpp,video/x-m4v,video/mp4,video/*';
1313
///
1414
/// This class implements the `package:image_picker` functionality for the web.
1515
class ImagePickerPlugin extends ImagePickerPlatform {
16-
final ImagePickerPluginTestOverrides _overrides;
16+
final ImagePickerPluginTestOverrides? _overrides;
1717
bool get _hasOverrides => _overrides != null;
1818

19-
html.Element _target;
19+
late html.Element _target;
2020

2121
/// A constructor that allows tests to override the function that creates file inputs.
2222
ImagePickerPlugin({
23-
@visibleForTesting ImagePickerPluginTestOverrides overrides,
23+
@visibleForTesting ImagePickerPluginTestOverrides? overrides,
2424
}) : _overrides = overrides {
2525
_target = _ensureInitialized(_kImagePickerInputsDomId);
2626
}
@@ -32,23 +32,23 @@ class ImagePickerPlugin extends ImagePickerPlatform {
3232

3333
@override
3434
Future<PickedFile> pickImage({
35-
@required ImageSource source,
36-
double maxWidth,
37-
double maxHeight,
38-
int imageQuality,
35+
required ImageSource source,
36+
double? maxWidth,
37+
double? maxHeight,
38+
int? imageQuality,
3939
CameraDevice preferredCameraDevice = CameraDevice.rear,
4040
}) {
41-
String capture = computeCaptureAttribute(source, preferredCameraDevice);
41+
String? capture = computeCaptureAttribute(source, preferredCameraDevice);
4242
return pickFile(accept: _kAcceptImageMimeType, capture: capture);
4343
}
4444

4545
@override
4646
Future<PickedFile> pickVideo({
47-
@required ImageSource source,
47+
required ImageSource source,
4848
CameraDevice preferredCameraDevice = CameraDevice.rear,
49-
Duration maxDuration,
49+
Duration? maxDuration,
5050
}) {
51-
String capture = computeCaptureAttribute(source, preferredCameraDevice);
51+
String? capture = computeCaptureAttribute(source, preferredCameraDevice);
5252
return pickFile(accept: _kAcceptVideoMimeType, capture: capture);
5353
}
5454

@@ -59,10 +59,11 @@ class ImagePickerPlugin extends ImagePickerPlatform {
5959
/// See https://caniuse.com/#feat=html-media-capture
6060
@visibleForTesting
6161
Future<PickedFile> pickFile({
62-
String accept,
63-
String capture,
62+
String? accept,
63+
String? capture,
6464
}) {
65-
html.FileUploadInputElement input = createInputElement(accept, capture);
65+
html.FileUploadInputElement input =
66+
createInputElement(accept, capture) as html.FileUploadInputElement;
6667
_injectAndActivate(input);
6768
return _getSelectedFile(input);
6869
}
@@ -73,25 +74,26 @@ class ImagePickerPlugin extends ImagePickerPlatform {
7374
///
7475
/// See: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file#capture
7576
@visibleForTesting
76-
String computeCaptureAttribute(ImageSource source, CameraDevice device) {
77+
String? computeCaptureAttribute(ImageSource source, CameraDevice device) {
7778
if (source == ImageSource.camera) {
7879
return (device == CameraDevice.front) ? 'user' : 'environment';
7980
}
8081
return null;
8182
}
8283

83-
html.File _getFileFromInput(html.FileUploadInputElement input) {
84+
html.File? _getFileFromInput(html.FileUploadInputElement input) {
8485
if (_hasOverrides) {
85-
return _overrides.getFileFromInput(input);
86+
return _overrides!.getFileFromInput(input);
8687
}
87-
return input?.files?.first;
88+
return input.files?.first;
8889
}
8990

9091
/// Handles the OnChange event from a FileUploadInputElement object
9192
/// Returns the objectURL of the selected file.
92-
String _handleOnChangeEvent(html.Event event) {
93-
final html.FileUploadInputElement input = event?.target;
94-
final html.File file = _getFileFromInput(input);
93+
String? _handleOnChangeEvent(html.Event event) {
94+
final html.FileUploadInputElement input =
95+
event.target as html.FileUploadInputElement;
96+
final html.File? file = _getFileFromInput(input);
9597

9698
if (file != null) {
9799
return html.Url.createObjectUrl(file);
@@ -105,7 +107,7 @@ class ImagePickerPlugin extends ImagePickerPlatform {
105107
// Observe the input until we can return something
106108
input.onChange.first.then((event) {
107109
final objectUrl = _handleOnChangeEvent(event);
108-
if (!_completer.isCompleted) {
110+
if (!_completer.isCompleted && objectUrl != null) {
109111
_completer.complete(PickedFile(objectUrl));
110112
}
111113
});
@@ -127,7 +129,7 @@ class ImagePickerPlugin extends ImagePickerPlatform {
127129
final html.Element targetElement =
128130
html.Element.tag('flt-image-picker-inputs')..id = id;
129131

130-
html.querySelector('body').children.add(targetElement);
132+
html.querySelector('body')!.children.add(targetElement);
131133
target = targetElement;
132134
}
133135
return target;
@@ -136,9 +138,9 @@ class ImagePickerPlugin extends ImagePickerPlatform {
136138
/// Creates an input element that accepts certain file types, and
137139
/// allows to `capture` from the device's cameras (where supported)
138140
@visibleForTesting
139-
html.Element createInputElement(String accept, String capture) {
141+
html.Element createInputElement(String? accept, String? capture) {
140142
if (_hasOverrides) {
141-
return _overrides.createInputElement(accept, capture);
143+
return _overrides!.createInputElement(accept, capture);
142144
}
143145

144146
html.Element element = html.FileUploadInputElement()..accept = accept;
@@ -162,22 +164,22 @@ class ImagePickerPlugin extends ImagePickerPlatform {
162164
/// A function that creates a file input with the passed in `accept` and `capture` attributes.
163165
@visibleForTesting
164166
typedef OverrideCreateInputFunction = html.Element Function(
165-
String accept,
166-
String capture,
167+
String? accept,
168+
String? capture,
167169
);
168170

169171
/// A function that extracts a [html.File] from the file `input` passed in.
170172
@visibleForTesting
171173
typedef OverrideExtractFilesFromInputFunction = html.File Function(
172-
html.Element input,
174+
html.Element? input,
173175
);
174176

175177
/// Overrides for some of the functionality above.
176178
@visibleForTesting
177179
class ImagePickerPluginTestOverrides {
178180
/// Override the creation of the input element.
179-
OverrideCreateInputFunction createInputElement;
181+
late OverrideCreateInputFunction createInputElement;
180182

181183
/// Override the extraction of the selected file from an input element.
182-
OverrideExtractFilesFromInputFunction getFileFromInput;
184+
late OverrideExtractFilesFromInputFunction getFileFromInput;
183185
}
Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
name: image_picker_for_web
22
description: Web platform implementation of image_picker
33
homepage: https://github.com/flutter/plugins/tree/master/packages/image_picker/image_picker_for_web
4-
# 0.1.y+z is compatible with 1.0.0, if you land a breaking change bump
5-
# the version to 2.0.0.
6-
# See more details: https://github.com/flutter/flutter/wiki/Package-migration-to-1.0.0
7-
version: 0.1.0+3
4+
5+
version: 2.0.0-nullsafety
86

97
flutter:
108
plugin:
@@ -14,19 +12,19 @@ flutter:
1412
fileName: image_picker_for_web.dart
1513

1614
dependencies:
17-
image_picker_platform_interface: ^1.1.0
15+
image_picker_platform_interface: ^2.0.0-nullsafety
1816
flutter:
1917
sdk: flutter
2018
flutter_web_plugins:
2119
sdk: flutter
22-
meta: ^1.1.7
23-
js: ^0.6.0
20+
meta: ^1.3.0-nullsafety.6
21+
js: ^0.6.3-nullsafety.3
2422

2523
dev_dependencies:
2624
flutter_test:
2725
sdk: flutter
28-
pedantic: ^1.8.0
26+
pedantic: ^1.10.0
2927

3028
environment:
31-
sdk: ">=2.5.0 <3.0.0"
29+
sdk: ">=2.12.0-0 <3.0.0"
3230
flutter: ">=1.10.0"

packages/image_picker/image_picker_for_web/test/image_picker_for_web_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ import 'package:image_picker_for_web/image_picker_for_web.dart';
1313
import 'package:image_picker_platform_interface/image_picker_platform_interface.dart';
1414

1515
final String expectedStringContents = "Hello, world!";
16-
final Uint8List bytes = utf8.encode(expectedStringContents);
16+
final Uint8List bytes = utf8.encode(expectedStringContents) as Uint8List;
1717
final html.File textFile = html.File([bytes], "hello.txt");
1818

1919
void main() {
2020
// Under test...
21-
ImagePickerPlugin plugin;
21+
late ImagePickerPlugin plugin;
2222

2323
setUp(() {
2424
plugin = ImagePickerPlugin();

0 commit comments

Comments
 (0)