0

The following code outputs the following results:

double firstValue = 1.2;
print('type of firstValue: ${firstValue.runtimeType}'); // double

double secondValue = 1;
print('type of secondValue: ${secondValue.runtimeType}'); // int

print('type of secondValue parsed: ${secondValue.toDouble().runtimeType}'); // int

Why a declared double value is understood like an int value? Even when I do an explicit cast, the the 1 value is still an int.

Why does this happen?

svprdga
  • 2,171
  • 1
  • 28
  • 51

1 Answers1

1

I presume that you see those results with dart2js (including DartPad).

JavaScript does not have separate int and double types; it uses IEEE-754 double-precision floating-point values everywhere. When Dart is transpiled to JavaScript, Dart's int and double types thus are both backed by the same type, and conversion back to Dart types requires heuristics since the type information was lost.

If you try your code with the Dart VM, you should see the results that you expect.

jamesdlin
  • 81,374
  • 13
  • 159
  • 204