toggleTorch method

Future<void> toggleTorch()

Switches the flashlight on or off.

Does nothing if the device has no torch, or if the camera is not running.

If the current torch state is TorchState.auto, the torch is turned on or off depending on its actual current state.

Implementation

Future<void> toggleTorch() async {
  _throwIfNotInitialized();

  if (!value.isRunning) {
    return;
  }

  final TorchState torchState = value.torchState;

  if (torchState == TorchState.unavailable) {
    return;
  }

  // Request the torch state to be switched to the opposite state.
  // When the platform has updated the torch state,
  // it will send an update through the torch state event stream.
  await MobileScannerPlatform.instance.toggleTorch();
}