Skip to content

Commit 6f3b078

Browse files
Chris Yangadsonpleal
Chris Yang
authored andcommitted
[image_picker_platform_interface] migrate to nnbd (flutter#3492)
1 parent 3bd33b5 commit 6f3b078

15 files changed

+68
-567
lines changed

packages/image_picker/image_picker_platform_interface/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## 2.0.0-nullsafety
2+
3+
* Migrate to null safety.
4+
* Breaking Changes:
5+
* Removed the deprecated methods: `ImagePickerPlatform.retrieveLostDataAsDartIoFile`,`ImagePickerPlatform.pickImagePath` and `ImagePickerPlatform.pickVideoPath`.
6+
* Removed deprecated class: `LostDataResponse`.
7+
18
## 1.1.6
29

310
* Fix test asset file location.

packages/image_picker/image_picker_platform_interface/lib/src/method_channel/method_channel_image_picker.dart

Lines changed: 24 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
// found in the LICENSE file.
44

55
import 'dart:async';
6-
import 'dart:io';
76

87
import 'package:flutter/foundation.dart';
98
import 'package:flutter/services.dart';
10-
import 'package:meta/meta.dart' show required, visibleForTesting;
9+
import 'package:meta/meta.dart' show visibleForTesting;
1110

1211
import 'package:image_picker_platform_interface/image_picker_platform_interface.dart';
1312

@@ -20,14 +19,14 @@ class MethodChannelImagePicker extends ImagePickerPlatform {
2019
MethodChannel get channel => _channel;
2120

2221
@override
23-
Future<PickedFile> pickImage({
24-
@required ImageSource source,
25-
double maxWidth,
26-
double maxHeight,
27-
int imageQuality,
22+
Future<PickedFile?> pickImage({
23+
required ImageSource source,
24+
double? maxWidth,
25+
double? maxHeight,
26+
int? imageQuality,
2827
CameraDevice preferredCameraDevice = CameraDevice.rear,
2928
}) async {
30-
String path = await pickImagePath(
29+
String? path = await _pickImagePath(
3130
source: source,
3231
maxWidth: maxWidth,
3332
maxHeight: maxHeight,
@@ -37,15 +36,13 @@ class MethodChannelImagePicker extends ImagePickerPlatform {
3736
return path != null ? PickedFile(path) : null;
3837
}
3938

40-
@override
41-
Future<String> pickImagePath({
42-
@required ImageSource source,
43-
double maxWidth,
44-
double maxHeight,
45-
int imageQuality,
39+
Future<String?> _pickImagePath({
40+
required ImageSource source,
41+
double? maxWidth,
42+
double? maxHeight,
43+
int? imageQuality,
4644
CameraDevice preferredCameraDevice = CameraDevice.rear,
4745
}) {
48-
assert(source != null);
4946
if (imageQuality != null && (imageQuality < 0 || imageQuality > 100)) {
5047
throw ArgumentError.value(
5148
imageQuality, 'imageQuality', 'must be between 0 and 100');
@@ -72,26 +69,24 @@ class MethodChannelImagePicker extends ImagePickerPlatform {
7269
}
7370

7471
@override
75-
Future<PickedFile> pickVideo({
76-
@required ImageSource source,
72+
Future<PickedFile?> pickVideo({
73+
required ImageSource source,
7774
CameraDevice preferredCameraDevice = CameraDevice.rear,
78-
Duration maxDuration,
75+
Duration? maxDuration,
7976
}) async {
80-
String path = await pickVideoPath(
77+
String? path = await _pickVideoPath(
8178
source: source,
8279
maxDuration: maxDuration,
8380
preferredCameraDevice: preferredCameraDevice,
8481
);
8582
return path != null ? PickedFile(path) : null;
8683
}
8784

88-
@override
89-
Future<String> pickVideoPath({
90-
@required ImageSource source,
85+
Future<String?> _pickVideoPath({
86+
required ImageSource source,
9187
CameraDevice preferredCameraDevice = CameraDevice.rear,
92-
Duration maxDuration,
88+
Duration? maxDuration,
9389
}) {
94-
assert(source != null);
9590
return _channel.invokeMethod<String>(
9691
'pickVideo',
9792
<String, dynamic>{
@@ -104,7 +99,7 @@ class MethodChannelImagePicker extends ImagePickerPlatform {
10499

105100
@override
106101
Future<LostData> retrieveLostData() async {
107-
final Map<String, dynamic> result =
102+
final Map<String, dynamic>? result =
108103
await _channel.invokeMapMethod<String, dynamic>('retrieve');
109104

110105
if (result == null) {
@@ -113,64 +108,28 @@ class MethodChannelImagePicker extends ImagePickerPlatform {
113108

114109
assert(result.containsKey('path') ^ result.containsKey('errorCode'));
115110

116-
final String type = result['type'];
111+
final String? type = result['type'];
117112
assert(type == kTypeImage || type == kTypeVideo);
118113

119-
RetrieveType retrieveType;
114+
RetrieveType? retrieveType;
120115
if (type == kTypeImage) {
121116
retrieveType = RetrieveType.image;
122117
} else if (type == kTypeVideo) {
123118
retrieveType = RetrieveType.video;
124119
}
125120

126-
PlatformException exception;
121+
PlatformException? exception;
127122
if (result.containsKey('errorCode')) {
128123
exception = PlatformException(
129124
code: result['errorCode'], message: result['errorMessage']);
130125
}
131126

132-
final String path = result['path'];
127+
final String? path = result['path'];
133128

134129
return LostData(
135130
file: path != null ? PickedFile(path) : null,
136131
exception: exception,
137132
type: retrieveType,
138133
);
139134
}
140-
141-
@override
142-
// ignore: deprecated_member_use_from_same_package
143-
Future<LostDataResponse> retrieveLostDataAsDartIoFile() async {
144-
final Map<String, dynamic> result =
145-
await _channel.invokeMapMethod<String, dynamic>('retrieve');
146-
if (result == null) {
147-
// ignore: deprecated_member_use_from_same_package
148-
return LostDataResponse.empty();
149-
}
150-
assert(result.containsKey('path') ^ result.containsKey('errorCode'));
151-
152-
final String type = result['type'];
153-
assert(type == kTypeImage || type == kTypeVideo);
154-
155-
RetrieveType retrieveType;
156-
if (type == kTypeImage) {
157-
retrieveType = RetrieveType.image;
158-
} else if (type == kTypeVideo) {
159-
retrieveType = RetrieveType.video;
160-
}
161-
162-
PlatformException exception;
163-
if (result.containsKey('errorCode')) {
164-
exception = PlatformException(
165-
code: result['errorCode'], message: result['errorMessage']);
166-
}
167-
168-
final String path = result['path'];
169-
170-
// ignore: deprecated_member_use_from_same_package
171-
return LostDataResponse(
172-
file: path == null ? null : File(path),
173-
exception: exception,
174-
type: retrieveType);
175-
}
176135
}

packages/image_picker/image_picker_platform_interface/lib/src/platform_interface/image_picker_platform.dart

Lines changed: 12 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import 'dart:async';
66

7-
import 'package:meta/meta.dart' show required;
87
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
98

109
import 'package:image_picker_platform_interface/src/method_channel/method_channel_image_picker.dart';
@@ -39,80 +38,6 @@ abstract class ImagePickerPlatform extends PlatformInterface {
3938
_instance = instance;
4039
}
4140

42-
/// Returns a [String] containing a path to the image that was picked.
43-
///
44-
/// The `source` argument controls where the image comes from. This can
45-
/// be either [ImageSource.camera] or [ImageSource.gallery].
46-
///
47-
/// If specified, the image will be at most `maxWidth` wide and
48-
/// `maxHeight` tall. Otherwise the image will be returned at it's
49-
/// original width and height.
50-
///
51-
/// The `imageQuality` argument modifies the quality of the image, ranging from 0-100
52-
/// where 100 is the original/max quality. If `imageQuality` is null, the image with
53-
/// the original quality will be returned. Compression is only supported for certain
54-
/// image types such as JPEG and on Android PNG and WebP, too. If compression is not supported for the image that is picked,
55-
/// a warning message will be logged.
56-
///
57-
/// Use `preferredCameraDevice` to specify the camera to use when the `source` is [ImageSource.camera].
58-
/// The `preferredCameraDevice` is ignored when `source` is [ImageSource.gallery]. It is also ignored if the chosen camera is not supported on the device.
59-
/// Defaults to [CameraDevice.rear].
60-
///
61-
/// In Android, the MainActivity can be destroyed for various reasons. If that happens, the result will be lost
62-
/// in this call. You can then call [retrieveLostDataAsDartIoFile] when your app relaunches to retrieve the lost data.
63-
@Deprecated('Use pickImage instead.')
64-
Future<String> pickImagePath({
65-
@required ImageSource source,
66-
double maxWidth,
67-
double maxHeight,
68-
int imageQuality,
69-
CameraDevice preferredCameraDevice = CameraDevice.rear,
70-
}) {
71-
throw UnimplementedError('legacyPickImage() has not been implemented.');
72-
}
73-
74-
/// Returns a [String] containing a path to the video that was picked.
75-
///
76-
/// The [source] argument controls where the video comes from. This can
77-
/// be either [ImageSource.camera] or [ImageSource.gallery].
78-
///
79-
/// The [maxDuration] argument specifies the maximum duration of the captured video. If no [maxDuration] is specified,
80-
/// the maximum duration will be infinite.
81-
///
82-
/// Use `preferredCameraDevice` to specify the camera to use when the `source` is [ImageSource.camera].
83-
/// The `preferredCameraDevice` is ignored when `source` is [ImageSource.gallery]. It is also ignored if the chosen camera is not supported on the device.
84-
/// Defaults to [CameraDevice.rear].
85-
///
86-
/// In Android, the MainActivity can be destroyed for various fo reasons. If that happens, the result will be lost
87-
/// in this call. You can then call [retrieveLostDataAsDartIoFile] when your app relaunches to retrieve the lost data.
88-
@Deprecated('Use pickVideo instead.')
89-
Future<String> pickVideoPath({
90-
@required ImageSource source,
91-
CameraDevice preferredCameraDevice = CameraDevice.rear,
92-
Duration maxDuration,
93-
}) {
94-
throw UnimplementedError('pickVideoPath() has not been implemented.');
95-
}
96-
97-
/// Retrieve the lost image file when [pickImagePath] or [pickVideoPath] failed because the MainActivity is destroyed. (Android only)
98-
///
99-
/// Image or video can be lost if the MainActivity is destroyed. And there is no guarantee that the MainActivity is always alive.
100-
/// Call this method to retrieve the lost data and process the data according to your APP's business logic.
101-
///
102-
/// Returns a [LostDataResponse] if successfully retrieved the lost data. The [LostDataResponse] can represent either a
103-
/// successful image/video selection, or a failure.
104-
///
105-
/// Calling this on a non-Android platform will throw [UnimplementedError] exception.
106-
///
107-
/// See also:
108-
/// * [LostDataResponse], for what's included in the response.
109-
/// * [Android Activity Lifecycle](https://developer.android.com/reference/android/app/Activity.html), for more information on MainActivity destruction.
110-
@Deprecated('Use retrieveLostData instead.')
111-
Future<LostDataResponse> retrieveLostDataAsDartIoFile() {
112-
throw UnimplementedError(
113-
'retrieveLostDataAsDartIoFile() has not been implemented.');
114-
}
115-
11641
// Next version of the API.
11742

11843
/// Returns a [PickedFile] with the image that was picked.
@@ -141,11 +66,13 @@ abstract class ImagePickerPlatform extends PlatformInterface {
14166
///
14267
/// In Android, the MainActivity can be destroyed for various reasons. If that happens, the result will be lost
14368
/// in this call. You can then call [retrieveLostData] when your app relaunches to retrieve the lost data.
144-
Future<PickedFile> pickImage({
145-
@required ImageSource source,
146-
double maxWidth,
147-
double maxHeight,
148-
int imageQuality,
69+
///
70+
/// If no images were picked, the return value is null.
71+
Future<PickedFile?> pickImage({
72+
required ImageSource source,
73+
double? maxWidth,
74+
double? maxHeight,
75+
int? imageQuality,
14976
CameraDevice preferredCameraDevice = CameraDevice.rear,
15077
}) {
15178
throw UnimplementedError('pickImage() has not been implemented.');
@@ -165,10 +92,12 @@ abstract class ImagePickerPlatform extends PlatformInterface {
16592
///
16693
/// In Android, the MainActivity can be destroyed for various fo reasons. If that happens, the result will be lost
16794
/// in this call. You can then call [retrieveLostData] when your app relaunches to retrieve the lost data.
168-
Future<PickedFile> pickVideo({
169-
@required ImageSource source,
95+
///
96+
/// If no images were picked, the return value is null.
97+
Future<PickedFile?> pickVideo({
98+
required ImageSource source,
17099
CameraDevice preferredCameraDevice = CameraDevice.rear,
171-
Duration maxDuration,
100+
Duration? maxDuration,
172101
}) {
173102
throw UnimplementedError('pickVideo() has not been implemented.');
174103
}

packages/image_picker/image_picker_platform_interface/lib/src/types/lost_data_response.dart

Lines changed: 0 additions & 52 deletions
This file was deleted.

packages/image_picker/image_picker_platform_interface/lib/src/types/picked_file/base.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ abstract class PickedFileBase {
5252
/// If `end` is present, only up to byte-index `end` will be read. Otherwise, until end of file.
5353
///
5454
/// In order to make sure that system resources are freed, the stream must be read to completion or the subscription on the stream must be cancelled.
55-
Stream<Uint8List> openRead([int start, int end]) {
55+
Stream<Uint8List> openRead([int? start, int? end]) {
5656
throw UnimplementedError('openRead() has not been implemented.');
5757
}
5858
}

packages/image_picker/image_picker_platform_interface/lib/src/types/picked_file/html.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@ import './base.dart';
1010
/// It wraps the bytes of a selected file.
1111
class PickedFile extends PickedFileBase {
1212
final String path;
13-
final Uint8List _initBytes;
13+
final Uint8List? _initBytes;
1414

1515
/// Construct a PickedFile object from its ObjectUrl.
1616
///
1717
/// Optionally, this can be initialized with `bytes`
1818
/// so no http requests are performed to retrieve files later.
19-
PickedFile(this.path, {Uint8List bytes})
19+
PickedFile(this.path, {Uint8List? bytes})
2020
: _initBytes = bytes,
2121
super(path);
2222

2323
Future<Uint8List> get _bytes async {
2424
if (_initBytes != null) {
25-
return Future.value(UnmodifiableUint8ListView(_initBytes));
25+
return Future.value(UnmodifiableUint8ListView(_initBytes!));
2626
}
2727
return http.readBytes(Uri.parse(path));
2828
}
@@ -38,7 +38,7 @@ class PickedFile extends PickedFileBase {
3838
}
3939

4040
@override
41-
Stream<Uint8List> openRead([int start, int end]) async* {
41+
Stream<Uint8List> openRead([int? start, int? end]) async* {
4242
final bytes = await _bytes;
4343
yield bytes.sublist(start ?? 0, end ?? bytes.length);
4444
}

0 commit comments

Comments
 (0)