Dart runtime error: type 'int' is not a subtype of type 'String'
type 'int' is not a subtype of type 'String'Short answer
Dart does not convert an int to a String for you. The error means a value that is really an int reached code that expects a String. It is usually a JSON field that holds a number while the model casts it as String. Convert with toString() or interpolation, or change the model's field type.
Fix it fast
1Find out which cause you have
Print the runtime type of the value that fails, taken from the decoded data, not from your model.
debugPrint('${json['id']} is ${json['id'].runtimeType}');2Match what you see, then jump to its fix
What does the error look like?
type 'int' is not a subtype of type 'String'Text widget given an int where a String is expected
Cause 1 of 2
A JSON number is read as a String
1Check
jsonDecode returns dynamic, so the real types are only known at runtime. A field the API sends as 12 is an int, even if your model says String.
final map = jsonDecode(body) as Map<String, dynamic>;
debugPrint(map.map((k, v) => MapEntry(k, v.runtimeType)).toString());2Fix
Either make the model field an int, or convert when reading. Pick one meaning for the field and keep to it.
// model field is a String
id = json['id'].toString();
// or model field is an int
id = json['id'] as int;3Verify
Decode a sample response in a unit test and check the model.
flutter testConfirmed by docs.flutter.dev
Cause 2 of 2
An int is passed where a String is required
1Check
Dart treats int and String as unrelated types; each conversion is explicit. Text(count) with an int count is such a case.
grep -rn "Text(" lib/ | grep -v "'"2Fix
Convert the number to a string with toString() or string interpolation.
Text(count.toString())
Text('$count')3Verify
Hot reload and open the screen.
flutter runConfirmed by dart.dev
How do I stop it coming back?
- Write a fromJson test for every model using a real sample response.
- Agree with the API owner whether ids are numbers or strings, and document it.
- Avoid dynamic maps in widgets; convert to typed models once, at the edge.
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