Dart Version 2.10.0

I am using flutter Dart with firestore as backend and have encountered a problem where a value of 1 for a field when stored (after json_seralizer) into a variable of type double, although succeeds, its runtime is still int.

This manifests into a runtime error when assigning the double variable value to another double variable with the error type 'int' is not a subtype of type 'double?' in type cast.

For example:- In Firestore -> rValue: 1

In Dart class -> double rateValue

Serialized through

rateValue = (rValue as num)?.toDouble()

Then later,

double currencyExchangeRate;
currencyExchangeRate = rateValue //throws error at runtime type 'int' is not a subtype of type 'double?' in type cast

Dartpad sample code to see the problem,

import 'dart:math' as math;
void main() {

  math.Random rand = math.Random();

  int x = rand.nextInt(10);

  double a = x.toDouble();
  double b = (x as num).toDouble();

  print ('a is $a and ${a.runtimeType} and b is $b and ${b.runtimeType}');


//Above code prints "a is 7 and int and b is 7 and int"

I have searched a lot of stackoverflow posts for same issue but none has helped so far.

Even with the explicit casting and no dynamic variables the double variables (a and b) still show a runtime type of int.

How can I successfully assign currencyExchangeRate = rateValue and prevent this runtime error?


Solution 1: Pawan

I have found the cause of the problem I particularly faced in my own flutter dart code.

Prior to json_serializer being available in dart I had my own manual casting from firestore data into the class and that code was not converting using the .toDouble() method. Hence, the value 1 was being treated as integer still and not as double.

The partial class that json_seralizer creates with fromJson and toJson methods correctly converts the figure 1 to double in the target class.

I have removed my own serializer code to now relying on json_serializer generated code in this instance (as I always do otherwise anyway).

So, no real issue was there with dart conversion, just my oversight in the investigation and I did't spot the fact that my own long-time-ago serializer code was the culprit.

Json_seralizer does the (e as num)?.toDouble() which correctly converts 1 to 1.0 in the target class.

Solution 2: botanyhelp

I believe that the previous answer about dartpad.dev transpiling to javascript is on the right track for an explanation. The code below, when run on dartpad.dev, running dart version 2.18.2, is telling us that the variable in question, x, is BOTH an int and a double, as shown in the output found below the code in the commented section:

class Point {
  double x;
  double y;
  Point(this.x, this.y);

void main() {
  Point point1 = Point(2.0, 4.0);
  print("point1 is at ${point1.x} and ${point1.y.toString()}");
  print("point1.x is ${point1.x is String ? '' : 'not '}String");
  print("point1.x is ${point1.x is double ? '' : 'not '}double");
  print("point1.x is ${point1.x is int ? '' : 'not '}int");
  print("point1.x is ${point1.x.runtimeType.toString()}");

// point1 is at 2 and 4
// point1.x is not String
// point1.x is double
// point1.x is int
// point1.x is int