stop method

Future<void> stop()

Stop the camera.

After calling this method, the camera can be restarted using start.

Does nothing if the camera is already stopped.

Implementation

Future<void> stop() async {
  // 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;
  }

  _disposeListeners();

  final TorchState oldTorchState = value.torchState;

  // After the camera stopped, set the torch state to off,
  // as the torch state callback is never called when the camera is stopped.
  // If the device does not have a torch, do not report "off".
  value = value.copyWith(
    isRunning: false,
    torchState: oldTorchState == TorchState.unavailable
        ? TorchState.unavailable
        : TorchState.off,
  );

  await MobileScannerPlatform.instance.stop();
}