Mohammed Nabil
Mohammed Nabil

Reputation: 802

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?

Upvotes: 0

Views: 8279

Answers (1)

Herry
Herry

Reputation: 199

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.

Upvotes: 5

Related Questions