Skip to content

Dart runtime error

Flutter: “type ‘List<dynamic>’ is not a subtype of type ‘List<int>’”

Parsing JSON or reading Firestore data throws type ‘List<dynamic>’ is not a subtype of type ‘List<int>’ (or List<String>, List<Map<String, dynamic>>). jsonDecode and most data APIs build lists as List<dynamic>, even when every item is an int. Assigning that list to a List<int> variable, or using as List<int>, fails at runtime.

Rather hand it over? App Fix Ticket: One reproducible bug in an existing mobile or web app, diagnosed and fixed.The Fix is $1,200 in total. The $450 Diagnosis is optional and credited toward it.

Full refund if Simon can’t take it on once he sees your project, or if you cancel before work starts. Refund terms.

Short answer

Convert the list instead of casting it: List<int>.from(json['ids']) or (json['ids'] as List).cast<int>(). For lists of objects, map each element: (json['items'] as List).map((e) => Item.fromJson(e as Map<String, dynamic>)).toList().

Rather not fix it yourself? App Fix Ticket fixes one reproducible bug at a fixed price, with an optional Diagnosis that is credited toward the fix.

Why the list has the wrong type, and the fix

  1. 01

    A JSON list assigned to a typed list

    “The elements iterable itself may have any element type, so this constructor can be used to down-cast a List” Dart, List.from constructor

    final List<int> ids = json['ids']; compiles but fails at runtime. List.from creates a new list of the right type and checks every element.

    final ids = List<int>.from(json['ids'] as List);
    final tags = List<String>.from(json['tags'] ?? const []);
  2. 02

    A list of objects

    Each element is a Map<String, dynamic>, so convert each one into your model with map and toList.

    final items = (json['items'] as List)
        .map((e) => Item.fromJson(e as Map<String, dynamic>))
        .toList();
  3. 03

    cast<int>() and mixed numbers

    “If any operation tries to read an element that is not an instance of R, the access will throw instead.” Dart, List.cast

    cast<int>() returns a typed view without copying. It throws when an element isn’t an int, which happens when the API sends 1.0 or a string. Normalize those first: for 1.0, use (e as num).toInt(); for numeric strings, use int.parse(e as String).

    final counts = (json['counts'] as List).map((e) => (e as num).toInt()).toList();

Checked against the vendor’s docs on : Dart API: List.from; Dart API: List.cast; Flutter: JSON and serialization.

Rather hand it over?

Fixed scope, fixed price, paid upfront. The work happens in writing; no call needed.

“Very responsive, attentive to fixing bugs, and completed the project as needed! Thank you!”
A buyer in the United States, July 2022. Shopify on Fiverr
Read 126 of the 127 Fiverr reviews

Related: Flutter “Map is not a subtype of type List” · Flutter “type ‘Null’ is not a subtype of type ‘String’” · Flutter “String can’t be assigned to Uri” · Flutter “Looking up a deactivated widget’s ancestor is unsafe”

Questions

More on this error

Should I use cast or List.from?

List.from copies and checks every element once, so errors show up where you parse. cast returns a view that checks on each read, so a bad element can fail later, far from the parsing code.

Does json_serializable avoid this?

Yes. Generated fromJson code converts typed lists for you, which is why Flutter’s JSON guide recommends code generation for medium to large projects.

Can you fix it for me?

If one bug is the whole problem, the App Fix Ticket covers it. If you already know the cause, you can pay for the fix directly; if not, start with a written diagnosis, which is credited toward the fix. If the app has more going on, the Launch Check is a written check of the whole app. Each issue found is rated critical, high, medium or low, with where it is and how it was confirmed. No issues worth fixing, no charge. If the Launch Check finds nothing rated medium or higher, Simon refunds the full $450. You keep the report either way. This applies to the first 10 Launch Checks bought after October 30, 2026.

A different error?

Ask Simon for a price range

Send the error and what you were doing. Simon replies by email, usually within one business day, with a price range.