Maaz Zindani

Loading the index…

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

Reading a Firestore number into a Dart double throws a type error

Errortype '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.

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

Fix it fast

  1. 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);
  2. 2Match what you see, then jump to its fix

    1. 1type 'int' is not a subtype of type 'double' on one document but not on anotherThe field holds a whole number, read as doubleFix
    2. 2The error appears when reading an array field, not a single numberA list or map of numbers is cast to double itemsFix

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

Did this fix it?

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

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.