@@ -29,60 +29,63 @@ class MyApp extends StatelessWidget {
2929}
3030
3131class 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
4040class _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
357360typedef void OnPickImageCallback (
358- double maxWidth, double maxHeight, int quality);
361+ double ? maxWidth, double ? maxHeight, int ? quality);
359362
360363class 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
369372class 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 {
0 commit comments