switchCamera method

Future<void> switchCamera([
  1. CameraFacing? facing
])

Switches to the camera with facing, or to the other one if null.

Does nothing where the device has no camera on that side (ScannerState.canSwitchCamera is false once a camera has opened); if that turns out only when the camera opens, the camera that was open opens again. A paused scanner stays paused. While the camera is closed, only the camera start opens changes.

Implementation

Future<void> switchCamera([CameraFacing? facing]) async {
  final CameraFacing previous = _facing;
  final CameraFacing target = facing ?? previous.other;
  if (target == previous || _otherFacing == false) {
    return;
  }
  _facing = target;
  final ScannerStatus status = value.status;
  if (status.hasCamera || status == ScannerStatus.starting) {
    final bool paused = _pausedNow;
    if (!await _restart(paused)) {
      return;
    }
    if (value.status == ScannerStatus.error &&
        value.error is CameraNotFoundException &&
        _facing == target) {
      // No camera there after all: back to the one that worked.
      _missing.add(target);
      _facing = previous;
      await _startUnlessHidden(paused);
    }
  } else {
    _set(value.copyWith(facing: target));
  }
}