Skip to content

Commit

Permalink
imp: add code check for stop
Browse files Browse the repository at this point in the history
  • Loading branch information
juliansteenbakker committed Jan 15, 2025
1 parent 9f602a3 commit 5c0d5e0
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions lib/src/mobile_scanner_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,14 @@ class MobileScannerController extends ValueNotifier<MobileScannerState> {
}
}

void _stop() {
/// Returns false if stop is called but not necessary, otherwise true is returned.
bool _stop() {
// Do nothing if not initialized or already stopped.
// On the web, the permission popup triggers a lifecycle change from resumed to inactive,
// due to the permission popup gaining focus.
// This would 'stop' the camera while it is not ready yet.
if (!value.isInitialized || !value.isRunning || _isDisposed) {
return;
return false;
}

_disposeListeners();
Expand All @@ -205,6 +206,7 @@ class MobileScannerController extends ValueNotifier<MobileScannerState> {
? TorchState.unavailable
: TorchState.off,
);
return true;
}

/// Analyze an image file.
Expand Down Expand Up @@ -360,8 +362,9 @@ class MobileScannerController extends ValueNotifier<MobileScannerState> {
///
/// Does nothing if the camera is already stopped.
Future<void> stop() async {
_stop();
await MobileScannerPlatform.instance.stop();
if (_stop()) {
await MobileScannerPlatform.instance.stop();
}
}

/// Pause the camera.
Expand All @@ -371,8 +374,9 @@ class MobileScannerController extends ValueNotifier<MobileScannerState> {
///
/// Does nothing if the camera is already paused or stopped.
Future<void> pause() async {
_stop();
await MobileScannerPlatform.instance.pause();
if (_stop()) {
await MobileScannerPlatform.instance.pause();
}
}

/// Switch between the front and back camera.
Expand Down

0 comments on commit 5c0d5e0

Please sign in to comment.