toBigIntValue function

BigInt toBigIntValue(
  1. Object? data, {
  2. BigInt? min,
  3. BigInt? max,
})

Converts data to BigInt or throws FormatException if cannot convert.

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

Implementation

BigInt toBigIntValue(Object? data, {BigInt? min, BigInt? max}) {
  if (data == null) throw const NullValueException();
  BigInt result;
  if (data is BigInt) {
    result = data;
  } else if (data is num) {
    result = BigInt.from(data);
  } else if (data is String) {
    result = BigInt.parse(data);
  } else if (data is bool) {
    result = data ? BigInt.one : BigInt.zero;
  } else {
    throw ConversionException(target: BigInt, data: data);
  }
  if (min != null && result < min) {
    result = min;
  }
  if (max != null && result > max) {
    result = max;
  }
  return result;
}