From d6358ad9ae4eae7da0d3197d12de06d3f5c5d85c Mon Sep 17 00:00:00 2001 From: lcuis Date: Tue, 10 Jan 2023 22:39:31 +0100 Subject: [PATCH 1/4] Proposed solution for #155 [BUG] iOS camera preview crashes when switching app --- lib/src/states/camera_picker_state.dart | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/src/states/camera_picker_state.dart b/lib/src/states/camera_picker_state.dart index 8f6f82b3..61c856ee 100644 --- a/lib/src/states/camera_picker_state.dart +++ b/lib/src/states/camera_picker_state.dart @@ -192,12 +192,16 @@ class CameraPickerState extends State @override void didChangeAppLifecycleState(AppLifecycleState state) { final CameraController? c = innerController; + if (c == null && state == AppLifecycleState.resumed) { + initCameras(currentCamera); + } // App state changed before we got the chance to initialize. if (c == null || !c.value.isInitialized) { return; } if (state == AppLifecycleState.inactive) { c.dispose(); + innerController = null; } else if (state == AppLifecycleState.resumed) { initCameras(currentCamera); } From 6ec256961881eb7acb0599b1490c6fa55ab2b236 Mon Sep 17 00:00:00 2001 From: lcuis Date: Fri, 13 Jan 2023 16:54:34 +0100 Subject: [PATCH 2/4] merged two null conditions Verified with mentioned scenarios that the fix continues to work on iPhone SE 2020. --- lib/src/states/camera_picker_state.dart | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/src/states/camera_picker_state.dart b/lib/src/states/camera_picker_state.dart index d3339caa..dec9473c 100644 --- a/lib/src/states/camera_picker_state.dart +++ b/lib/src/states/camera_picker_state.dart @@ -199,11 +199,11 @@ class CameraPickerState extends State @override void didChangeAppLifecycleState(AppLifecycleState state) { final CameraController? c = innerController; - if (c == null && state == AppLifecycleState.resumed) { - initCameras(currentCamera); - } - // App state changed before we got the chance to initialize. if (c == null || !c.value.isInitialized) { + if (state == AppLifecycleState.resumed) { + initCameras(currentCamera); + } + // App state changed before we got the chance to initialize. return; } if (state == AppLifecycleState.inactive) { From 5e6a40af24470999992b963015cbb28344ed3ef4 Mon Sep 17 00:00:00 2001 From: Alex Li Date: Sun, 15 Jan 2023 22:08:26 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=F0=9F=8E=A8=20Format?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/src/states/camera_picker_state.dart | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/lib/src/states/camera_picker_state.dart b/lib/src/states/camera_picker_state.dart index dec9473c..faf6fbf6 100644 --- a/lib/src/states/camera_picker_state.dart +++ b/lib/src/states/camera_picker_state.dart @@ -199,18 +199,14 @@ class CameraPickerState extends State @override void didChangeAppLifecycleState(AppLifecycleState state) { final CameraController? c = innerController; - if (c == null || !c.value.isInitialized) { - if (state == AppLifecycleState.resumed) { - initCameras(currentCamera); - } + if (state == AppLifecycleState.resumed) { + initCameras(currentCamera); + } else if (c == null || !c.value.isInitialized) { // App state changed before we got the chance to initialize. return; - } - if (state == AppLifecycleState.inactive) { + } else if (state == AppLifecycleState.inactive) { c.dispose(); innerController = null; - } else if (state == AppLifecycleState.resumed) { - initCameras(currentCamera); } } From e95725a51685245cdad924dbf57ec50cda7f4c90 Mon Sep 17 00:00:00 2001 From: Alex Li Date: Mon, 16 Jan 2023 11:16:03 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=F0=9F=93=9D=20CHANGELOG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 810b36f8..eca54980 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,10 @@ that can be found in the LICENSE file. --> - Allow flash modes failed to switch and can move on to next when switching. (#156) +### Fixes + +- Fix lifecycle integrations with the camera preview. (#157) + ## 3.6.5 ### Fixes