Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

FormatException: Invalid radix-10 number when parsing a string to int

ErrorUnhandled 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.

  • Applies to Dart 3.9, Flutter 3.35
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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));
  2. 2Match what you see, then jump to its fix

    1. 1(at character 1) with an empty string, a label, or null turned into the text 'null'The text is empty or not a numberFix
    2. 2The value looks like 3.5 or 1e3 in the logThe text has a decimal pointFix
    3. 3The text contains letters a-f, or starts with 0xThe digits are not base 10Fix

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'));  // 42

Confirmed by api.dart.dev

Did this fix it?

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'));   // null

2Fix

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();   // 3

3Verify

Check both results.

print([double.parse('3.5'), double.parse('3.5').toInt()]);

Confirmed by api.dart.dev

Did this fix it?

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));   // 255

2Fix

Tell the parser the base.

final n = int.parse('ff', radix: 16);   // 255
final m = int.parse('0xff');            // 255

3Verify

Compare with toRadixString, which round-trips for a valid radix.

print(int.parse(255.toRadixString(16), radix: 16));

Confirmed by api.dart.dev

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.