@@ -13,14 +13,14 @@ final String _kAcceptVideoMimeType = 'video/3gpp,video/x-m4v,video/mp4,video/*';
13
13
///
14
14
/// This class implements the `package:image_picker` functionality for the web.
15
15
class ImagePickerPlugin extends ImagePickerPlatform {
16
- final ImagePickerPluginTestOverrides _overrides;
16
+ final ImagePickerPluginTestOverrides ? _overrides;
17
17
bool get _hasOverrides => _overrides != null ;
18
18
19
- html.Element _target;
19
+ late html.Element _target;
20
20
21
21
/// A constructor that allows tests to override the function that creates file inputs.
22
22
ImagePickerPlugin ({
23
- @visibleForTesting ImagePickerPluginTestOverrides overrides,
23
+ @visibleForTesting ImagePickerPluginTestOverrides ? overrides,
24
24
}) : _overrides = overrides {
25
25
_target = _ensureInitialized (_kImagePickerInputsDomId);
26
26
}
@@ -32,23 +32,23 @@ class ImagePickerPlugin extends ImagePickerPlatform {
32
32
33
33
@override
34
34
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,
39
39
CameraDevice preferredCameraDevice = CameraDevice .rear,
40
40
}) {
41
- String capture = computeCaptureAttribute (source, preferredCameraDevice);
41
+ String ? capture = computeCaptureAttribute (source, preferredCameraDevice);
42
42
return pickFile (accept: _kAcceptImageMimeType, capture: capture);
43
43
}
44
44
45
45
@override
46
46
Future <PickedFile > pickVideo ({
47
- @ required ImageSource source,
47
+ required ImageSource source,
48
48
CameraDevice preferredCameraDevice = CameraDevice .rear,
49
- Duration maxDuration,
49
+ Duration ? maxDuration,
50
50
}) {
51
- String capture = computeCaptureAttribute (source, preferredCameraDevice);
51
+ String ? capture = computeCaptureAttribute (source, preferredCameraDevice);
52
52
return pickFile (accept: _kAcceptVideoMimeType, capture: capture);
53
53
}
54
54
@@ -59,10 +59,11 @@ class ImagePickerPlugin extends ImagePickerPlatform {
59
59
/// See https://caniuse.com/#feat=html-media-capture
60
60
@visibleForTesting
61
61
Future <PickedFile > pickFile ({
62
- String accept,
63
- String capture,
62
+ String ? accept,
63
+ String ? capture,
64
64
}) {
65
- html.FileUploadInputElement input = createInputElement (accept, capture);
65
+ html.FileUploadInputElement input =
66
+ createInputElement (accept, capture) as html.FileUploadInputElement ;
66
67
_injectAndActivate (input);
67
68
return _getSelectedFile (input);
68
69
}
@@ -73,25 +74,26 @@ class ImagePickerPlugin extends ImagePickerPlatform {
73
74
///
74
75
/// See: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file#capture
75
76
@visibleForTesting
76
- String computeCaptureAttribute (ImageSource source, CameraDevice device) {
77
+ String ? computeCaptureAttribute (ImageSource source, CameraDevice device) {
77
78
if (source == ImageSource .camera) {
78
79
return (device == CameraDevice .front) ? 'user' : 'environment' ;
79
80
}
80
81
return null ;
81
82
}
82
83
83
- html.File _getFileFromInput (html.FileUploadInputElement input) {
84
+ html.File ? _getFileFromInput (html.FileUploadInputElement input) {
84
85
if (_hasOverrides) {
85
- return _overrides.getFileFromInput (input);
86
+ return _overrides! .getFileFromInput (input);
86
87
}
87
- return input? .files? .first;
88
+ return input.files? .first;
88
89
}
89
90
90
91
/// Handles the OnChange event from a FileUploadInputElement object
91
92
/// 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);
95
97
96
98
if (file != null ) {
97
99
return html.Url .createObjectUrl (file);
@@ -105,7 +107,7 @@ class ImagePickerPlugin extends ImagePickerPlatform {
105
107
// Observe the input until we can return something
106
108
input.onChange.first.then ((event) {
107
109
final objectUrl = _handleOnChangeEvent (event);
108
- if (! _completer.isCompleted) {
110
+ if (! _completer.isCompleted && objectUrl != null ) {
109
111
_completer.complete (PickedFile (objectUrl));
110
112
}
111
113
});
@@ -127,7 +129,7 @@ class ImagePickerPlugin extends ImagePickerPlatform {
127
129
final html.Element targetElement =
128
130
html.Element .tag ('flt-image-picker-inputs' )..id = id;
129
131
130
- html.querySelector ('body' ).children.add (targetElement);
132
+ html.querySelector ('body' )! .children.add (targetElement);
131
133
target = targetElement;
132
134
}
133
135
return target;
@@ -136,9 +138,9 @@ class ImagePickerPlugin extends ImagePickerPlatform {
136
138
/// Creates an input element that accepts certain file types, and
137
139
/// allows to `capture` from the device's cameras (where supported)
138
140
@visibleForTesting
139
- html.Element createInputElement (String accept, String capture) {
141
+ html.Element createInputElement (String ? accept, String ? capture) {
140
142
if (_hasOverrides) {
141
- return _overrides.createInputElement (accept, capture);
143
+ return _overrides! .createInputElement (accept, capture);
142
144
}
143
145
144
146
html.Element element = html.FileUploadInputElement ()..accept = accept;
@@ -162,22 +164,22 @@ class ImagePickerPlugin extends ImagePickerPlatform {
162
164
/// A function that creates a file input with the passed in `accept` and `capture` attributes.
163
165
@visibleForTesting
164
166
typedef OverrideCreateInputFunction = html.Element Function (
165
- String accept,
166
- String capture,
167
+ String ? accept,
168
+ String ? capture,
167
169
);
168
170
169
171
/// A function that extracts a [html.File] from the file `input` passed in.
170
172
@visibleForTesting
171
173
typedef OverrideExtractFilesFromInputFunction = html.File Function (
172
- html.Element input,
174
+ html.Element ? input,
173
175
);
174
176
175
177
/// Overrides for some of the functionality above.
176
178
@visibleForTesting
177
179
class ImagePickerPluginTestOverrides {
178
180
/// Override the creation of the input element.
179
- OverrideCreateInputFunction createInputElement;
181
+ late OverrideCreateInputFunction createInputElement;
180
182
181
183
/// Override the extraction of the selected file from an input element.
182
- OverrideExtractFilesFromInputFunction getFileFromInput;
184
+ late OverrideExtractFilesFromInputFunction getFileFromInput;
183
185
}
0 commit comments