parse static method
Parses a dynamic value v
into an integer.
Tries to convert the dynamic value v
into an integer. It supports parsing
from int, BigInt, List<int>
, and String types. If v
is a String and
represents a hexadecimal number (prefixed with '0x' or not), it is parsed
accordingly.
Parameters:
v
: The dynamic value to be parsed into an integer.
Returns:
- An integer representation of the parsed value.
Implementation
static int parse(dynamic v) {
try {
if (v is int) return v;
if (v is BigInt) return v.toInt();
if (v is List<int>) {
return fromBytes(v, sign: true);
}
if (v is String) {
int? parse = int.tryParse(v);
if (parse == null && StringUtils.ixHexaDecimalNumber(v)) {
parse = int.parse(StringUtils.strip0x(v), radix: 16);
}
return parse!;
}
} catch (_) {}
throw const ArgumentException("invalid input for parse int");
}