@@ -307,6 +307,7 @@ class CameraPickerState extends State<CameraPicker>
307
307
/// 当前相机实例的控制器
308
308
CameraController get controller => _controller! ;
309
309
CameraController ? _controller;
310
+ bool _shouldLockInitialize = false ;
310
311
311
312
/// Available cameras.
312
313
/// 可用的相机实例
@@ -477,7 +478,7 @@ class CameraPickerState extends State<CameraPicker>
477
478
}
478
479
if (state == AppLifecycleState .inactive) {
479
480
c.dispose ();
480
- } else if (state == AppLifecycleState .resumed && ! c.value.isInitialized ) {
481
+ } else if (state == AppLifecycleState .resumed && ! _shouldLockInitialize ) {
481
482
// Drop initialize when the controller has been already initialized.
482
483
// This will typically resolve the lifecycle issue on iOS when permissions
483
484
// are requested for the first time.
@@ -513,6 +514,7 @@ class CameraPickerState extends State<CameraPicker>
513
514
final CameraController ? _c = _controller;
514
515
// Then unbind the controller from widgets, which requires a build frame.
515
516
safeSetState (() {
517
+ _shouldLockInitialize = true ;
516
518
_maxAvailableZoom = 1 ;
517
519
_minAvailableZoom = 1 ;
518
520
_currentZoom = 1 ;
@@ -541,6 +543,7 @@ class CameraPickerState extends State<CameraPicker>
541
543
// After cameras fetched, judge again with the list is empty or not to
542
544
// ensure there is at least an available camera for use.
543
545
if (cameraDescription == null && (cameras.isEmpty)) {
546
+ _shouldLockInitialize = false ;
544
547
handleErrorWithHandler (
545
548
CameraException (
546
549
'No CameraDescription found.' ,
@@ -601,7 +604,9 @@ class CameraPickerState extends State<CameraPicker>
601
604
} catch (e) {
602
605
handleErrorWithHandler (e, widget.onError);
603
606
} finally {
604
- safeSetState (() {});
607
+ safeSetState (() {
608
+ _shouldLockInitialize = false ;
609
+ });
605
610
}
606
611
});
607
612
}
@@ -791,7 +796,10 @@ class CameraPickerState extends State<CameraPicker>
791
796
final XFile _file = await controller.takePicture ();
792
797
// Delay disposing the controller to hold the preview.
793
798
Future <void >.delayed (const Duration (milliseconds: 500 ), () {
794
- controller.dispose ();
799
+ _controller? .dispose ();
800
+ safeSetState (() {
801
+ _controller = null ;
802
+ });
795
803
});
796
804
final AssetEntity ? entity = await CameraPickerViewer .pushToViewer (
797
805
context,
0 commit comments