setExposureOffset method Null safety

Future<double> setExposureOffset(
  1. double offset
)

Sets the exposure offset for the selected camera.

The supplied offset value should be in EV units. 1 EV unit represents a doubling in brightness. It should be between the minimum and maximum offsets obtained through getMinExposureOffset and getMaxExposureOffset respectively. Throws a CameraException when an illegal offset is supplied.

When the supplied offset value does not align with the step size obtained through getExposureStepSize, it will automatically be rounded to the nearest step.

Returns the (rounded) offset value that was set.

Implementation

Future<double> setExposureOffset(double offset) async {
  _throwIfNotInitialized('setExposureOffset');
  // Check if offset is in range
  final List<double> range = await Future.wait(
      <Future<double>>[getMinExposureOffset(), getMaxExposureOffset()]);
  if (offset < range[0] || offset > range[1]) {
    throw CameraException(
      'exposureOffsetOutOfBounds',
      'The provided exposure offset was outside the supported range for this device.',
    );
  }

  // Round to the closest step if needed
  final double stepSize = await getExposureOffsetStepSize();
  if (stepSize > 0) {
    final double inv = 1.0 / stepSize;
    double roundedOffset = (offset * inv).roundToDouble() / inv;
    if (roundedOffset > range[1]) {
      roundedOffset = (offset * inv).floorToDouble() / inv;
    } else if (roundedOffset < range[0]) {
      roundedOffset = (offset * inv).ceilToDouble() / inv;
    }
    offset = roundedOffset;
  }

  try {
    return CameraPlatform.instance.setExposureOffset(_cameraId, offset);
  } on PlatformException catch (e) {
    throw CameraException(e.code, e.message);
  }
}