Maaz Zindani

Loading the index…

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

Flutter error: The argument type 'String' can't be assigned to the parameter type 'int'

ErrorThe argument type 'String' can't be assigned to the parameter type 'int'.

Short answer

Dart checks argument types at compile time and never turns text into a number for you. A value read from a TextField, a JSON map or storage is a String, so passing it where an int is expected fails. Convert it with int.parse or int.tryParse, or fix the declared type if the String was never meant to be a number.

  • Applies to Flutter 3.3x stable, Dart 3.x
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Run the analyzer. It prints the file and line, and the line shows which argument is the String.

    dart analyze
  2. 2Match what you see, then jump to its fix

    1. 1The flagged argument comes from controller.text, a query string, or a decoded body fieldText from an input or a response is passed where a number is neededFix
    2. 2The String is correct, but the receiving parameter says int, for example a model field that stores an id or a codeThe parameter or field is declared with the wrong typeFix
    3. 3The error is a runtime type error such as type 'String' is not a subtype of type 'int', after you added an as cast to silence the analyzerA dynamic value is cast to the wrong typeFix

What does the error look like?

  • The argument type 'String' can't be assigned to the parameter type 'int'.
  • A value of type 'String' can't be assigned to a variable of type 'int'.

Cause 1 of 3

Text from an input or a response is passed where a number is needed

1Check

Text field controllers, URL parts and many API fields are Strings even when they look like digits.

dart analyze lib

2Fix

Convert the value. Use int.tryParse when the text may be empty or wrong, because it returns null instead of throwing; int.parse throws a FormatException on invalid input.

final age = int.tryParse(_ageController.text);
if (age == null) {
  // show a validation message
  return;
}
setAge(age);

3Verify

dart analyze reports no error on that line, and an invalid entry shows your message instead of crashing.

dart analyze lib

Confirmed by api.dart.dev

Did this fix it?

Cause 2 of 3

The parameter or field is declared with the wrong type

1Check

The analyzer lists making the types compatible as the first fix: change the declaration so the static types agree.

grep -rn "int " lib/models

2Fix

Change the parameter or field to String, or change the caller to hold an int from the start. Convert at the edge where data enters the app, not in every widget.

// before
void show(int code) {}
// after
void show(String code) {}

3Verify

Analyze again; the argument and parameter types match.

dart analyze lib

Confirmed by dart.dev

Did this fix it?

Cause 3 of 3

A dynamic value is cast to the wrong type

1Check

Dart documents the as cast as a fix only when you are sure of the runtime type; if the value is a String, the cast throws.

dart analyze lib

2Fix

Replace the cast with a type test and a conversion, so each possible runtime type is handled.

final raw = json['count'];
final count = raw is int ? raw : int.tryParse('$raw') ?? 0;

3Verify

Run with a String value and an int value in the data; both give a number.

flutter test

Confirmed by dart.dev

Did this fix it?

How do I stop it coming back?

  • Parse input once, where it enters the app, and pass typed values after that.
  • Prefer int.tryParse for anything a user typed.
  • Run dart analyze in CI so type errors never reach a build.

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.