Reading a Firestore number into a Dart double throws a type error
type 'int' is not a subtype of type 'double'Short answer
Firestore stores integers and floating-point numbers as separate types, and Dart keeps int and double apart too. A value saved as 5 comes back as an int, so a cast or assignment to double fails at runtime. Read it as num and call toDouble(). The same applies to lists of numbers.
Fix it fast
1Find out which cause you have
Print the runtime type of the field that fails. If it says int, the data in Firestore is an integer.
print(snapshot.data()!['price'].runtimeType);2Match what you see, then jump to its fix
What does the error look like?
type 'int' is not a subtype of type 'double'TypeError
Cause 1 of 2
The field holds a whole number, read as double
1Check
Firestore lists a 64-bit integer type and a double-precision floating-point type separately. A document written with 5 holds an integer, one written with 5.5 holds a float. Both are valid numbers, so the field type varies by document.
final v = snapshot.data()!['price'];
print('${v.runtimeType}');2Fix
Read the field as num, which is the supertype of both int and double, then call toDouble().
final price = (snapshot.data()!['price'] as num).toDouble();3Verify
The same code runs on documents that hold 5 and documents that hold 5.5.
print(price);Confirmed by firebase.google.com
Cause 2 of 2
A list or map of numbers is cast to double items
1Check
A cast such as as List<double> is checked at run time and fails when any item is an int. Dart's docs say a failed cast to a more specific collection type throws.
print((data['scores'] as List).map((e) => e.runtimeType).toSet());2Fix
Cast the list to List<num> and convert each item.
final scores = (data['scores'] as List)
.map((e) => (e as num).toDouble())
.toList();3Verify
The result is a List<double> and no error is thrown.
print(scores.runtimeType);Confirmed by dart.dev
How do I stop it coming back?
- Treat every number coming from Firestore as num and convert it once, at the edge, in fromJson or fromFirestore.
- Write money as an integer amount of the smallest unit, so it never changes type.
- Do not rely on a literal fixing it: an integer literal is converted to double only in code, not in data that already arrived.
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
Dart throws 'List<dynamic> is not a subtype of type Map<String, dynamic>'
Error: List<dynamic> is not a subtype of type Map<String, dynamic>
jsonDecode returns a List<dynamic> for a JSON array and a Map<String, dynamic> for a JSON object.
3 causes, each with a fixMobile 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 fix