Skip to content

Commit 3d70464

Browse files
authored
[image_picker] Migrate to null-safety (flutter#3524)
Migrate image_picker to null-safety
1 parent 0cec317 commit 3d70464

File tree

8 files changed

+79
-498
lines changed

8 files changed

+79
-498
lines changed

packages/image_picker/image_picker/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 0.7.0-nullsafety
2+
* Migrate to nullsafety
3+
* Breaking Changes:
4+
* Removed the deprecated methods: `ImagePicker.pickImage`, `ImagePicker.pickVideo`,
5+
`ImagePicker.retrieveLostData`
6+
17
## 0.6.7+22
28

39
* iOS: update XCUITests to separate each test session.

packages/image_picker/image_picker/example/lib/main.dart

Lines changed: 50 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -29,60 +29,63 @@ class MyApp extends StatelessWidget {
2929
}
3030

3131
class MyHomePage extends StatefulWidget {
32-
MyHomePage({Key key, this.title}) : super(key: key);
32+
MyHomePage({Key? key, this.title}) : super(key: key);
3333

34-
final String title;
34+
final String? title;
3535

3636
@override
3737
_MyHomePageState createState() => _MyHomePageState();
3838
}
3939

4040
class _MyHomePageState extends State<MyHomePage> {
41-
PickedFile _imageFile;
41+
PickedFile? _imageFile;
4242
dynamic _pickImageError;
4343
bool isVideo = false;
44-
VideoPlayerController _controller;
45-
VideoPlayerController _toBeDisposed;
46-
String _retrieveDataError;
44+
VideoPlayerController? _controller;
45+
VideoPlayerController? _toBeDisposed;
46+
String? _retrieveDataError;
4747

4848
final ImagePicker _picker = ImagePicker();
4949
final TextEditingController maxWidthController = TextEditingController();
5050
final TextEditingController maxHeightController = TextEditingController();
5151
final TextEditingController qualityController = TextEditingController();
5252

53-
Future<void> _playVideo(PickedFile file) async {
53+
Future<void> _playVideo(PickedFile? file) async {
5454
if (file != null && mounted) {
5555
await _disposeVideoController();
56+
late VideoPlayerController controller;
5657
if (kIsWeb) {
57-
_controller = VideoPlayerController.network(file.path);
58-
// In web, most browsers won't honor a programmatic call to .play
59-
// if the video has a sound track (and is not muted).
60-
// Mute the video so it auto-plays in web!
61-
// This is not needed if the call to .play is the result of user
62-
// interaction (clicking on a "play" button, for example).
63-
await _controller.setVolume(0.0);
58+
controller = VideoPlayerController.network(file.path);
6459
} else {
65-
_controller = VideoPlayerController.file(File(file.path));
66-
await _controller.setVolume(1.0);
60+
controller = VideoPlayerController.file(File(file.path));
6761
}
68-
await _controller.initialize();
69-
await _controller.setLooping(true);
70-
await _controller.play();
62+
_controller = controller;
63+
// In web, most browsers won't honor a programmatic call to .play
64+
// if the video has a sound track (and is not muted).
65+
// Mute the video so it auto-plays in web!
66+
// This is not needed if the call to .play is the result of user
67+
// interaction (clicking on a "play" button, for example).
68+
final double volume = kIsWeb ? 0.0 : 1.0;
69+
await controller.setVolume(volume);
70+
await controller.initialize();
71+
await controller.setLooping(true);
72+
await controller.play();
7173
setState(() {});
7274
}
7375
}
7476

75-
void _onImageButtonPressed(ImageSource source, {BuildContext context}) async {
77+
void _onImageButtonPressed(ImageSource source,
78+
{BuildContext? context}) async {
7679
if (_controller != null) {
77-
await _controller.setVolume(0.0);
80+
await _controller!.setVolume(0.0);
7881
}
7982
if (isVideo) {
80-
final PickedFile file = await _picker.getVideo(
83+
final PickedFile? file = await _picker.getVideo(
8184
source: source, maxDuration: const Duration(seconds: 10));
8285
await _playVideo(file);
8386
} else {
84-
await _displayPickImageDialog(context,
85-
(double maxWidth, double maxHeight, int quality) async {
87+
await _displayPickImageDialog(context!,
88+
(double? maxWidth, double? maxHeight, int? quality) async {
8689
try {
8790
final pickedFile = await _picker.getImage(
8891
source: source,
@@ -105,8 +108,8 @@ class _MyHomePageState extends State<MyHomePage> {
105108
@override
106109
void deactivate() {
107110
if (_controller != null) {
108-
_controller.setVolume(0.0);
109-
_controller.pause();
111+
_controller!.setVolume(0.0);
112+
_controller!.pause();
110113
}
111114
super.deactivate();
112115
}
@@ -122,14 +125,14 @@ class _MyHomePageState extends State<MyHomePage> {
122125

123126
Future<void> _disposeVideoController() async {
124127
if (_toBeDisposed != null) {
125-
await _toBeDisposed.dispose();
128+
await _toBeDisposed!.dispose();
126129
}
127130
_toBeDisposed = _controller;
128131
_controller = null;
129132
}
130133

131134
Widget _previewVideo() {
132-
final Text retrieveError = _getRetrieveErrorWidget();
135+
final Text? retrieveError = _getRetrieveErrorWidget();
133136
if (retrieveError != null) {
134137
return retrieveError;
135138
}
@@ -146,18 +149,18 @@ class _MyHomePageState extends State<MyHomePage> {
146149
}
147150

148151
Widget _previewImage() {
149-
final Text retrieveError = _getRetrieveErrorWidget();
152+
final Text? retrieveError = _getRetrieveErrorWidget();
150153
if (retrieveError != null) {
151154
return retrieveError;
152155
}
153156
if (_imageFile != null) {
154157
if (kIsWeb) {
155158
// Why network?
156159
// See https://pub.dev/packages/image_picker#getting-ready-for-the-web-platform
157-
return Image.network(_imageFile.path);
160+
return Image.network(_imageFile!.path);
158161
} else {
159162
return Semantics(
160-
child: Image.file(File(_imageFile.path)),
163+
child: Image.file(File(_imageFile!.path)),
161164
label: 'image_picker_example_picked_image');
162165
}
163166
} else if (_pickImageError != null) {
@@ -189,15 +192,15 @@ class _MyHomePageState extends State<MyHomePage> {
189192
});
190193
}
191194
} else {
192-
_retrieveDataError = response.exception.code;
195+
_retrieveDataError = response.exception!.code;
193196
}
194197
}
195198

196199
@override
197200
Widget build(BuildContext context) {
198201
return Scaffold(
199202
appBar: AppBar(
200-
title: Text(widget.title),
203+
title: Text(widget.title!),
201204
),
202205
body: Center(
203206
child: !kIsWeb && defaultTargetPlatform == TargetPlatform.android
@@ -288,9 +291,9 @@ class _MyHomePageState extends State<MyHomePage> {
288291
);
289292
}
290293

291-
Text _getRetrieveErrorWidget() {
294+
Text? _getRetrieveErrorWidget() {
292295
if (_retrieveDataError != null) {
293-
final Text result = Text(_retrieveDataError);
296+
final Text result = Text(_retrieveDataError!);
294297
_retrieveDataError = null;
295298
return result;
296299
}
@@ -336,13 +339,13 @@ class _MyHomePageState extends State<MyHomePage> {
336339
TextButton(
337340
child: const Text('PICK'),
338341
onPressed: () {
339-
double width = maxWidthController.text.isNotEmpty
342+
double? width = maxWidthController.text.isNotEmpty
340343
? double.parse(maxWidthController.text)
341344
: null;
342-
double height = maxHeightController.text.isNotEmpty
345+
double? height = maxHeightController.text.isNotEmpty
343346
? double.parse(maxHeightController.text)
344347
: null;
345-
int quality = qualityController.text.isNotEmpty
348+
int? quality = qualityController.text.isNotEmpty
346349
? int.parse(qualityController.text)
347350
: null;
348351
onPick(width, height, quality);
@@ -355,40 +358,40 @@ class _MyHomePageState extends State<MyHomePage> {
355358
}
356359

357360
typedef void OnPickImageCallback(
358-
double maxWidth, double maxHeight, int quality);
361+
double? maxWidth, double? maxHeight, int? quality);
359362

360363
class AspectRatioVideo extends StatefulWidget {
361364
AspectRatioVideo(this.controller);
362365

363-
final VideoPlayerController controller;
366+
final VideoPlayerController? controller;
364367

365368
@override
366369
AspectRatioVideoState createState() => AspectRatioVideoState();
367370
}
368371

369372
class AspectRatioVideoState extends State<AspectRatioVideo> {
370-
VideoPlayerController get controller => widget.controller;
373+
VideoPlayerController? get controller => widget.controller;
371374
bool initialized = false;
372375

373376
void _onVideoControllerUpdate() {
374377
if (!mounted) {
375378
return;
376379
}
377-
if (initialized != controller.value.initialized) {
378-
initialized = controller.value.initialized;
380+
if (initialized != controller!.value.isInitialized) {
381+
initialized = controller!.value.isInitialized;
379382
setState(() {});
380383
}
381384
}
382385

383386
@override
384387
void initState() {
385388
super.initState();
386-
controller.addListener(_onVideoControllerUpdate);
389+
controller!.addListener(_onVideoControllerUpdate);
387390
}
388391

389392
@override
390393
void dispose() {
391-
controller.removeListener(_onVideoControllerUpdate);
394+
controller!.removeListener(_onVideoControllerUpdate);
392395
super.dispose();
393396
}
394397

@@ -397,8 +400,8 @@ class AspectRatioVideoState extends State<AspectRatioVideo> {
397400
if (initialized) {
398401
return Center(
399402
child: AspectRatio(
400-
aspectRatio: controller.value?.aspectRatio,
401-
child: VideoPlayer(controller),
403+
aspectRatio: controller!.value.aspectRatio,
404+
child: VideoPlayer(controller!),
402405
),
403406
);
404407
} else {

packages/image_picker/image_picker/example/pubspec.yaml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,23 @@ description: Demonstrates how to use the image_picker plugin.
33
author: Flutter Team <[email protected]>
44

55
dependencies:
6-
video_player: ^0.10.3
6+
video_player: ^2.0.0-nullsafety.7
77
flutter:
88
sdk: flutter
9-
flutter_plugin_android_lifecycle: ^1.0.2
9+
flutter_plugin_android_lifecycle: ^2.0.0-nullsafety.2
1010
image_picker:
1111
path: ../
12-
image_picker_for_web: ^0.1.0
1312

1413
dev_dependencies:
1514
flutter_driver:
1615
sdk: flutter
1716
integration_test:
1817
path: ../../../integration_test
19-
pedantic: ^1.8.0
18+
pedantic: ^1.10.0
2019

2120
flutter:
2221
uses-material-design: true
2322

2423
environment:
25-
sdk: ">=2.0.0-dev.28.0 <3.0.0"
24+
sdk: ">=2.12.0-0 <3.0.0"
2625
flutter: ">=1.10.0 <2.0.0"

packages/image_picker/image_picker/integration_test/old_image_picker_test.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// @dart=2.9
2+
13
import 'package:integration_test/integration_test.dart';
24

35
void main() {

0 commit comments

Comments
 (0)