switchCamera method

Future<void> switchCamera()

Switch between the front and back camera.

Does nothing if the device has less than 2 cameras.

Implementation

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

  final int? availableCameras = value.availableCameras;

  // Do nothing if the amount of cameras is less than 2 cameras.
  // If the the current platform does not provide the amount of cameras,
  // continue anyway.
  if (availableCameras != null && availableCameras < 2) {
    return;
  }

  await stop();

  final CameraFacing cameraDirection = value.cameraDirection;

  await start(
    cameraDirection: cameraDirection == CameraFacing.front
        ? CameraFacing.back
        : CameraFacing.front,
  );
}