'Unhandled Exception: FormatException: Invalid radix-10 number (at character 1) in Flutter

I am trying to convert string to int but it is throwing an exception "Unhandled Exception: FormatException: Invalid radix-10 number (at character 1)"

String aa = "627a32b69018c4b90f77af19";
int.parse(aa);

How to solve this issue?



Solution 1:[1]

The passed string is not a number as it contains non-digit characters like a or f.

It seems that you like to parse the input string as a hexadecimal string, in that case provide a radix (number base) value in your code.

The following snipped yields the output of 3.047725939849963e+28.

void main() {
  String aa = "627a32b69018c4b90f77af19";
  int? test = int.parse(aa, radix: 16);
  
  print("$test");
}

However, your number seems to be very large (10^28). There the result exceeds the range of an int, which is 64 bit at most, 32 bit for JavaScript. (more)

In these cases it is best to use BigInt, which supports arbitrarily large integer numbers.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1