From 8b74c4e1a443881ab137b151d22dbed2117c55c8 Mon Sep 17 00:00:00 2001 From: Vadim <44135514+vodemn@users.noreply.github.com> Date: Fri, 19 May 2023 11:12:10 +0200 Subject: [PATCH] ML-72 buildPreview() was called on a disposed CameraController (#73) * added `DeinitializeEvent` * typo --- .../bloc_container_camera.dart | 41 ++++++++++--------- .../event_container_camera.dart | 4 +- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/lib/screens/metering/components/camera_container/bloc_container_camera.dart b/lib/screens/metering/components/camera_container/bloc_container_camera.dart index da1c5ce..d70e6a2 100644 --- a/lib/screens/metering/components/camera_container/bloc_container_camera.dart +++ b/lib/screens/metering/components/camera_container/bloc_container_camera.dart @@ -23,8 +23,7 @@ import 'package:lightmeter/utils/log_2.dart'; class CameraContainerBloc extends EvSourceBlocBase { final MeteringInteractor _meteringInteractor; late final _WidgetsBindingObserver _observer; - CameraController? _cameraController; - CameraController? get cameraController => _cameraController; + late CameraController _cameraController; static const _maxZoom = 7.0; RangeValues? _zoomRange; @@ -50,6 +49,7 @@ class CameraContainerBloc extends EvSourceBlocBase(_onRequestPermission); on(_onOpenAppSettings); on(_onInitialize); + on(_onDeinitialize); on(_onZoomChanged); on(_onExposureOffsetChanged); on(_onExposureOffsetResetEvent); @@ -60,7 +60,7 @@ class CameraContainerBloc extends EvSourceBlocBase close() async { WidgetsBinding.instance.removeObserver(_observer); - unawaited(_cameraController?.dispose()); + unawaited(_cameraController.dispose()); communicationBloc.add(communication_event.MeteringEndedEvent(_ev100)); return super.close(); } @@ -118,30 +118,30 @@ class CameraContainerBloc extends EvSourceBlocBase([ - _cameraController!.getMinZoomLevel(), - _cameraController!.getMaxZoomLevel(), + _cameraController.getMinZoomLevel(), + _cameraController.getMaxZoomLevel(), ]).then((levels) => RangeValues(levels[0], math.min(_maxZoom, levels[1]))); _currentZoom = _zoomRange!.start; _exposureOffsetRange = await Future.wait([ - _cameraController!.getMinExposureOffset(), - _cameraController!.getMaxExposureOffset(), + _cameraController.getMinExposureOffset(), + _cameraController.getMaxExposureOffset(), ]).then( (levels) => RangeValues( math.max(_exposureMaxRange.start, levels[0]), math.min(_exposureMaxRange.end, levels[1]), ), ); - await _cameraController!.getExposureOffsetStepSize().then((value) { + await _cameraController.getExposureOffsetStepSize().then((value) { _exposureStep = value == 0 ? 0.1 : value; }); _currentExposureOffset = 0.0; - emit(CameraInitializedState(_cameraController!)); + emit(CameraInitializedState(_cameraController)); _emitActiveState(emit); } catch (e) { @@ -149,14 +149,19 @@ class CameraContainerBloc extends EvSourceBlocBase _onDeinitialize(DeinitializeEvent _, Emitter emit) async { + emit(const CameraLoadingState()); + unawaited(_cameraController.dispose()); + } + Future _onZoomChanged(ZoomChangedEvent event, Emitter emit) async { - _cameraController!.setZoomLevel(event.value); + _cameraController.setZoomLevel(event.value); _currentZoom = event.value; _emitActiveState(emit); } Future _onExposureOffsetChanged(ExposureOffsetChangedEvent event, Emitter emit) async { - _cameraController!.setExposureOffset(event.value); + _cameraController.setExposureOffset(event.value); _currentExposureOffset = event.value; _emitActiveState(emit); } @@ -178,13 +183,12 @@ class CameraContainerBloc extends EvSourceBlocBase !(_cameraController == null || - !_cameraController!.value.isInitialized || - _cameraController!.value.isTakingPicture); + bool get _canTakePhoto => + !(!_cameraController.value.isInitialized || _cameraController.value.isTakingPicture); Future _takePhoto() async { try { - final file = await _cameraController!.takePicture(); + final file = await _cameraController.takePicture(); final Uint8List bytes = await file.readAsBytes(); Directory(file.path).deleteSync(recursive: true); @@ -213,8 +217,7 @@ class CameraContainerBloc extends EvSourceBlocBase