Flutter error: The argument type 'String' can't be assigned to the parameter type 'int'
The 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.
Fix it fast
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 analyze2Match what you see, then jump to its fix
- 1
The 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
The 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
The 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
- 1
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 lib2Fix
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 libConfirmed by api.dart.dev
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/models2Fix
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 libConfirmed by dart.dev
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 lib2Fix
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 testConfirmed by dart.dev
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:
- 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