From 4ef945a8c16742742a4d315d28bb1ea9a48065c3 Mon Sep 17 00:00:00 2001 From: Marvin M <39344769+M123-dev@users.noreply.github.com> Date: Sun, 13 Feb 2022 09:38:38 +0100 Subject: [PATCH] fix: Camera not activating directly (#1125) * fix: Camera not activating directly * Update ml_kit_scan_page.dart * Update ml_kit_scan_page.dart * Update ml_kit_scan_page.dart --- .../lib/pages/scan/ml_kit_scan_page.dart | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/smooth_app/lib/pages/scan/ml_kit_scan_page.dart b/packages/smooth_app/lib/pages/scan/ml_kit_scan_page.dart index 02181ca1a4f..8b6fe35587f 100644 --- a/packages/smooth_app/lib/pages/scan/ml_kit_scan_page.dart +++ b/packages/smooth_app/lib/pages/scan/ml_kit_scan_page.dart @@ -129,16 +129,19 @@ class MLKitScannerPageState extends State { _controller?.setFocusMode(FocusMode.auto); _controller?.lockCaptureOrientation(DeviceOrientation.portraitUp); - _controller = _controller; - - try { - await _controller!.initialize(); + // If the controller is initialized update the UI. + _controller?.addListener(() { if (mounted) { setState(() {}); } if (_controller!.value.hasError) { + // TODO(M123): Handle errors better debugPrint(_controller!.value.errorDescription); } + }); + + try { + await _controller?.initialize(); _controller?.startImageStream(_processCameraImage); } on CameraException catch (e) { if (kDebugMode) { @@ -146,7 +149,6 @@ class MLKitScannerPageState extends State { debugPrint(e.toString()); } } - if (mounted) { setState(() {}); } @@ -154,7 +156,9 @@ class MLKitScannerPageState extends State { Future _stopImageStream() async { stoppingCamera = true; - setState(() {}); + if (mounted) { + setState(() {}); + } await _controller?.dispose(); _controller = null; }