FormatException: Invalid radix-10 number when parsing a string to int
Unhandled Exception: FormatException: Invalid radix-10 number (at character 1)Short answer
int.parse accepts only a whole-number literal. Anything else, such as empty text, a decimal point, letters or a hex value, throws FormatException. Print the exact string first, then use int.tryParse and handle null, use double.parse for decimals, or pass the right radix.
Fix it fast
1Find out which cause you have
Print the string with visible quotes, so empty values and stray characters show up, before you parse it.
print('value: "$text" length: ${text.length}'); print(int.tryParse(text));2Match what you see, then jump to its fix
What does the error look like?
Unhandled Exception: FormatException: Invalid radix-10 number (at character 1)FormatException: Invalid radix-10 number
Cause 1 of 3
The text is empty or not a number
1Check
int.parse treats its input as a possibly signed integer literal and throws FormatException when it is not one. A TextField that is still empty is the usual source.
print(int.tryParse(text) == null ? 'not an int: "$text"' : 'ok');2Fix
Use tryParse and decide what to do with null, instead of catching the exception.
final value = int.tryParse(text);
if (value == null) {
// show a validation message or use a default
return;
}
print(value + 1);3Verify
Run with an empty string and with '42'. The first takes the null branch, the second prints 43.
print(int.tryParse('')); // null
print(int.tryParse('42')); // 42Confirmed by api.dart.dev
Cause 2 of 3
The text has a decimal point
1Check
A whole-number parser rejects a fraction. The error position is the first character it could not accept.
print(int.tryParse('3.5')); // null2Fix
Parse with double.parse, which accepts a decimal point and an exponent, then convert if you need a whole number.
final d = double.parse('3.5');
final whole = d.toInt(); // 33Verify
Check both results.
print([double.parse('3.5'), double.parse('3.5').toInt()]);Confirmed by api.dart.dev
Cause 3 of 3
The digits are not base 10
1Check
The default radix is 10. With the default radix, digits may start with 0x; for other bases, pass the radix (2 to 36).
print(int.tryParse('ff')); // null
print(int.tryParse('ff', radix: 16)); // 2552Fix
Tell the parser the base.
final n = int.parse('ff', radix: 16); // 255
final m = int.parse('0xff'); // 2553Verify
Compare with toRadixString, which round-trips for a valid radix.
print(int.parse(255.toRadixString(16), radix: 16));Confirmed by api.dart.dev
How do I stop it coming back?
- Prefer int.tryParse for any text that comes from a user, a file or an API.
- Validate form fields before parsing, and show a message when the text is empty.
- Log the raw string next to the error so the bad value is visible.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- Qist BazaarA buy-now-pay-later marketplace built from a blank repo, now past 100K downloads on Google Play.
- SessionBirdAn omnichannel engagement suite in Flutter: campaigns, chatbot, tickets, analytics and live calling.
Related fixes
Mobile apps
AudioCache has no play method after updating audioplayers
The method 'play' isn't defined for the type 'AudioCache'.
Since audioplayers 1.0, AudioCache only turns asset files into local files and caches them.
2 causes, each with a fixMobile apps
Flutter Bloc throws that emit was called after the event handler completed
emit was called after an event handler completed normally.
Each on<Event> handler gets its own Emitter, and it stops working the moment the handler's returned Future completes.
2 causes, each with a fixMobile apps
analysis_options.yaml: the URI it includes can't be found
include_file_not_found
The analyzer reports include_file_not_found when the file named after include: in analysis_options.yaml does not resolve.
2 causes, each with a fixMobile apps
Dart error: the body might complete normally and return null for a non-nullable type
body_might_complete_normally
Dart found a path through your function or callback that reaches the closing brace without a return.
3 causes, each with a fixMobile apps
Dart throws ConcurrentModificationError while looping over a list
ConcurrentModificationError
Dart throws this when a collection changes size while something iterates it.
2 causes, each with a fixMobile apps
Dart cannot turn a compact timestamp like 20201101153000 into a DateTime
FormatException: Invalid date format
DateTime.parse accepts a subset of ISO 8601, and a time part must be introduced by T or a space.
2 causes, each with a fix