coerceIn method

T coerceIn(
  1. T minimumValue, [
  2. T? maximumValue
])

Ensures that this value lies in the specified range minimumValue..maximumValue.

@return this value if it's in the range, or minimumValue if this value is less than minimumValue, or maximumValue if this value is greater than maximumValue.

Implementation

T coerceIn(T minimumValue, [T? maximumValue]) {
  if (maximumValue != null && minimumValue > maximumValue) {
    throw ArgumentError(
      'Cannot coerce value to an empty range: '
      'maximum $maximumValue is less than minimum $minimumValue.',
    );
  }
  if (this < minimumValue) return minimumValue;
  if (maximumValue != null && this > maximumValue) return maximumValue;
  return this;
}