From 436f5b4c4868315c1307e99d676c4564fad6a599 Mon Sep 17 00:00:00 2001 From: Alex Li Date: Sun, 15 May 2022 14:42:43 +0800 Subject: [PATCH 1/7] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20Only=20update=20the=20?= =?UTF-8?q?controller=20if=20it's=20initialized=20successfully?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/camera/camera/example/lib/main.dart | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/camera/camera/example/lib/main.dart b/packages/camera/camera/example/lib/main.dart index 34942ba5aa77..9b7cc3c7ef0c 100644 --- a/packages/camera/camera/example/lib/main.dart +++ b/packages/camera/camera/example/lib/main.dart @@ -651,8 +651,6 @@ class _CameraExampleHomeState extends State imageFormatGroup: ImageFormatGroup.jpeg, ); - controller = cameraController; - // If the controller is updated then update the UI. cameraController.addListener(() { if (mounted) { @@ -666,6 +664,12 @@ class _CameraExampleHomeState extends State try { await cameraController.initialize(); + if (cameraController.value.hasError) { + showInSnackBar( + 'Camera error ${cameraController.value.errorDescription}', + ); + return; + } await Future.wait(>[ // The exposure mode is currently not supported on the web. ...!kIsWeb @@ -684,6 +688,7 @@ class _CameraExampleHomeState extends State .getMinZoomLevel() .then((double value) => _minAvailableZoom = value), ]); + controller = cameraController; } on CameraException catch (e) { switch (e.code) { case 'CameraAccessDenied': From 23a03f98d1d3e9609a92924f5ba88c00296bc8c1 Mon Sep 17 00:00:00 2001 From: Alex Li Date: Sun, 15 May 2022 14:42:58 +0800 Subject: [PATCH 2/7] =?UTF-8?q?=F0=9F=8E=A8=20Format?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/camera/camera/example/lib/main.dart | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/packages/camera/camera/example/lib/main.dart b/packages/camera/camera/example/lib/main.dart index 9b7cc3c7ef0c..9592dc2da49a 100644 --- a/packages/camera/camera/example/lib/main.dart +++ b/packages/camera/camera/example/lib/main.dart @@ -658,7 +658,8 @@ class _CameraExampleHomeState extends State } if (cameraController.value.hasError) { showInSnackBar( - 'Camera error ${cameraController.value.errorDescription}'); + 'Camera error ${cameraController.value.errorDescription}', + ); } }); @@ -672,15 +673,14 @@ class _CameraExampleHomeState extends State } await Future.wait(>[ // The exposure mode is currently not supported on the web. - ...!kIsWeb - ? >[ - cameraController.getMinExposureOffset().then( - (double value) => _minAvailableExposureOffset = value), - cameraController - .getMaxExposureOffset() - .then((double value) => _maxAvailableExposureOffset = value) - ] - : >[], + if (kIsWeb) ...>[ + cameraController + .getMinExposureOffset() + .then((double value) => _minAvailableExposureOffset = value), + cameraController + .getMaxExposureOffset() + .then((double value) => _maxAvailableExposureOffset = value) + ], cameraController .getMaxZoomLevel() .then((double value) => _maxAvailableZoom = value), From 264dab6764ab16731743cb4f4ae3e996ebad1eb1 Mon Sep 17 00:00:00 2001 From: Alex Li Date: Sun, 15 May 2022 14:45:07 +0800 Subject: [PATCH 3/7] =?UTF-8?q?=F0=9F=94=96=200.9.5+1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/camera/camera/CHANGELOG.md | 4 ++++ packages/camera/camera/pubspec.yaml | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/camera/camera/CHANGELOG.md b/packages/camera/camera/CHANGELOG.md index 8d713c60c276..e97b767656ed 100644 --- a/packages/camera/camera/CHANGELOG.md +++ b/packages/camera/camera/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.9.5+1 + +* Improve the example to update the controller only if it's initialized successfully. + ## 0.9.5 * Adds camera access permission handling logic on iOS to fix a related crash when using the camera for the first time. diff --git a/packages/camera/camera/pubspec.yaml b/packages/camera/camera/pubspec.yaml index 59cde43dd66a..14acf32e2324 100644 --- a/packages/camera/camera/pubspec.yaml +++ b/packages/camera/camera/pubspec.yaml @@ -4,7 +4,7 @@ description: A Flutter plugin for controlling the camera. Supports previewing Dart. repository: https://github.com/flutter/plugins/tree/main/packages/camera/camera issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+camera%22 -version: 0.9.5 +version: 0.9.5+1 environment: sdk: ">=2.14.0 <3.0.0" From 89ff1447ac136dfc3a3e79d4db742ebb0ad4e1a9 Mon Sep 17 00:00:00 2001 From: Alex Li Date: Fri, 20 May 2022 12:18:47 +0800 Subject: [PATCH 4/7] =?UTF-8?q?=F0=9F=94=A5=20Remove=20condition=20and=20a?= =?UTF-8?q?dd=20comments?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/camera/camera/example/lib/main.dart | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/packages/camera/camera/example/lib/main.dart b/packages/camera/camera/example/lib/main.dart index 9592dc2da49a..c6434417cfa6 100644 --- a/packages/camera/camera/example/lib/main.dart +++ b/packages/camera/camera/example/lib/main.dart @@ -665,12 +665,6 @@ class _CameraExampleHomeState extends State try { await cameraController.initialize(); - if (cameraController.value.hasError) { - showInSnackBar( - 'Camera error ${cameraController.value.errorDescription}', - ); - return; - } await Future.wait(>[ // The exposure mode is currently not supported on the web. if (kIsWeb) ...>[ @@ -688,6 +682,11 @@ class _CameraExampleHomeState extends State .getMinZoomLevel() .then((double value) => _minAvailableZoom = value), ]); + // `controller` needs to be set until it's fully initialized, to avoid a + // race condition when the lifecycle callback being called and creates a + // new `controller`. This happens when the camera permission dialog + // dismisses at the first run, which triggers `didChangeAppLifecycleState` + // that disposes and re-creates the controller. controller = cameraController; } on CameraException catch (e) { switch (e.code) { From dfb46c8337b5dbd7bd6204a461d06de3f985f255 Mon Sep 17 00:00:00 2001 From: Alex Li Date: Mon, 23 May 2022 11:38:35 +0800 Subject: [PATCH 5/7] =?UTF-8?q?=F0=9F=92=A1=20Update=20comment?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/camera/camera/example/lib/main.dart | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/camera/camera/example/lib/main.dart b/packages/camera/camera/example/lib/main.dart index c6434417cfa6..c05c80f5dcb2 100644 --- a/packages/camera/camera/example/lib/main.dart +++ b/packages/camera/camera/example/lib/main.dart @@ -682,11 +682,13 @@ class _CameraExampleHomeState extends State .getMinZoomLevel() .then((double value) => _minAvailableZoom = value), ]); - // `controller` needs to be set until it's fully initialized, to avoid a - // race condition when the lifecycle callback being called and creates a - // new `controller`. This happens when the camera permission dialog + // `controller` needs to be set until it's fully initialized to avoid a + // race condition when the lifecycle callback is being called and creates + // a new `controller`. This happens when the camera permission dialog // dismisses at the first run, which triggers `didChangeAppLifecycleState` - // that disposes and re-creates the controller. + // that disposes and re-creates the controller. Switching controllers in a + // short circumstance might cause the camera preview flashes or make the + // state hard to manage. controller = cameraController; } on CameraException catch (e) { switch (e.code) { From d9c2e6182b4a17c8f57045e5e4d39d026790ee55 Mon Sep 17 00:00:00 2001 From: Alex Li Date: Mon, 23 May 2022 11:40:20 +0800 Subject: [PATCH 6/7] =?UTF-8?q?=F0=9F=8E=A8=20Trailing=20comma?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/camera/camera/example/lib/main.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/camera/camera/example/lib/main.dart b/packages/camera/camera/example/lib/main.dart index c05c80f5dcb2..f4861e2b040d 100644 --- a/packages/camera/camera/example/lib/main.dart +++ b/packages/camera/camera/example/lib/main.dart @@ -673,7 +673,7 @@ class _CameraExampleHomeState extends State .then((double value) => _minAvailableExposureOffset = value), cameraController .getMaxExposureOffset() - .then((double value) => _maxAvailableExposureOffset = value) + .then((double value) => _maxAvailableExposureOffset = value), ], cameraController .getMaxZoomLevel() From 764d0b41f6c44bf0d7cee2d82bf6f077e234735d Mon Sep 17 00:00:00 2001 From: Alex Li Date: Mon, 23 May 2022 14:47:59 +0800 Subject: [PATCH 7/7] =?UTF-8?q?=F0=9F=9A=91=EF=B8=8F=20Fix=20reverted=20we?= =?UTF-8?q?b=20condition?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/camera/camera/example/lib/main.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/camera/camera/example/lib/main.dart b/packages/camera/camera/example/lib/main.dart index 50cc6428f66c..2f208a9444c6 100644 --- a/packages/camera/camera/example/lib/main.dart +++ b/packages/camera/camera/example/lib/main.dart @@ -667,7 +667,7 @@ class _CameraExampleHomeState extends State await cameraController.initialize(); await Future.wait(>[ // The exposure mode is currently not supported on the web. - if (kIsWeb) ...>[ + if (!kIsWeb) ...>[ cameraController .getMinExposureOffset() .then((double value) => _minAvailableExposureOffset = value),