toIntValue function

int toIntValue(
  1. Object? data, {
  2. int? min,
  3. int? max,
})

Converts data to int or throws FormatException if cannot convert.

If provided min and max are used to clamp the returned value.

Implementation

int toIntValue(Object? data, {int? min, int? max}) {
  if (data == null) throw const NullValueException();
  int result;
  if (data is num) {
    result = data.round();
  } else if (data is BigInt) {
    result = data.toInt();
  } else if (data is String) {
    result = _stringToInt(data);
  } else if (data is bool) {
    result = data ? 1 : 0;
  } else {
    throw ConversionException(target: int, data: data);
  }
  if (min != null && result < min) {
    result = min;
  }
  if (max != null && result > max) {
    result = max;
  }
  return result;
}