ensureNumber function

int ensureNumber(
  1. dynamic val
)

Implementation

int ensureNumber(dynamic val) {
  assertType(val, [int, String]);
  if (val is int) {
    return val;
  }

  final res = int.tryParse(val);
  if (res == null || res.isNaN) {
    throw ArgumentError("Invalid number string.");
  }

  return res;
}