[Solved] FormatException: Invalid radix-10 number Error in Flutter

In this post, we are going to show you how to solve "The following FormatException was thrown building Builder: Invalid radix-10 number" error in Flutter, this error happens when you are parsing a string to an integer.

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞══════════════
The following FormatException was thrown building Builder:
Invalid radix-10 number (at character 1)
Hafce00453dfg234fdksd
^

The relevant error-causing widget was:
  MaterialApp MaterialApp:file:///E:/flutter_apps/test/lib/main.dart:10:12

When the exception was thrown, this was the stack:
#0      int._handleFormatError (dart:core-patch/integers_patch.dart:131:5)
#1      int._parseRadix (dart:core-patch/integers_patch.dart:157:16)
#2      int._parse (dart:core-patch/integers_patch.dart:103:12)
#3      int.parse (dart:core-patch/integers_patch.dart:65:12)

This error happens when you use the invalid format of string on int.parse() function.

To solve this error, use the following method to get numbers only from String, this error will not happen if any kind or any format of String has.

String str = "Hafce00453dfg234fdksd";

//get the integer
int newnum1 = int.parse(str.replaceAll(RegExp(r'[^0-9]'),''));
print(newnum1); //output: 453234

//int newnum2 = int.parse(str);
//dont do this: Error: Invalid radix-10 number

Use the regex to extract numbers only from string and then convert them to integers so that the above error won't happen on your app.

In this way, you can solve the "FormatException: Invalid radix-10 number" error in Dart or Flutter.

No any Comments on this Article


Please Wait...