Why can't I convert a Number into a Double?

人走茶凉 提交于 2020-07-18 21:08:08

问题


weight is a field (Number in Firestore), set as 100.

int weight = json['weight'];
double weight = json['weight'];

int weight works fine, returns 100 as expected, but double weight crashes (Object.noSuchMethod exception) rather than returning 100.0, which is what I expected.

However, the following works:

num weight = json['weight'];
num.toDouble();

回答1:


When parsing 100 from Firestore (which actually does not support a "number type", but converts it), it will by standard be parsed to an int.
Dart does not automatically "smartly" cast those types. In fact, you cannot cast an int to a double, which is the problem you are facing. If it were possible, your code would just work fine.

Parsing

Instead, you can parse it yourself:

double weight = json['weight'].toDouble();

Casting

What also works, is parsing the JSON to a num and then assigning it to a double, which will cast num to double.

double weight = json['weight'] as num;

This seems a bit odd at first and in fact the Dart Analysis tool (which is e.g. built in into the Dart plugin for VS Code and IntelliJ) will mark it as an "unnecessary cast", which it is not.

double a = 100; // this will not compile

double b = 100 as num; // this will compile, but is still marked as an "unnecessary cast"

double b = 100 as num compiles because num is the super class of double and Dart casts super to sub types even without explicit casts.
An explicit cast would be the follwing:

double a = 100 as double; // does not compile because int is not the super class of double

double b = (100 as num) as double; // compiles, you can also omit the double cast

Here is a nice read about "Types and casting in Dart".

Explanation

What happened to you is the following:

double weight;

weight = 100; // cannot compile because 100 is considered an int
// is the same as
weight = 100 as double; // which cannot work as I explained above
// Dart adds those casts automatically



回答2:


You can do it in one line:

 double weight = (json['weight'] as num).toDouble();



回答3:


You can Parse the data Like given below:

Here document is a Map<String,dynamic>

double opening = double.tryParse(document['opening'].toString());



回答4:


In Dart, int and double are separate types, both subtypes of num.

There is no automatic conversion between number types. If you write:

num n = 100;
double d = n;

you will get a run-time error. Dart's static type system allows unsafe down-casts, so the unsafe assignment of n to d (unsafe because not all num values are double values) is treated implicitly as:

num n = 100;
double d = n as double;

The as double checks that the value is actually a double (or null), and throws if it isn't. If that check succeeds, then it can safely assign the value to d since it is known to match the variable's type.

That's what's happening here. The actual value of json['weight'] (likely with static type Object or dynamic) is the int object with value 100. Assigning that to int works. Assigning it to num works. Assigning it to double throws.

The Dart JSON parser parses numbers as integers if they have no decimal or exponent parts (0.0 is a double, 0e0 is a double, 0 is an integer). That's very convenient in most cases, but occasionally annoying in cases like yours where you want a double, but the code creating the JSON didn't write it as a double.

In cases like that, you just have to write .toDouble() on the values when you extract them. That's a no-op on actual doubles.

As a side note, Dart compiled to JavaScript represents all numbers as the JavaScript Number type, which means that all numbers are doubles. In JS compiled code, all integers can be assigned to double without conversion. That will not work when the code is run on a non-JS implementation, like Flutter, Dart VM/server or ahead-of-time compilation for iOS, so don't depend on it, or your code will not be portable.




回答5:


Simply convert int to double like this

int a = 10;
double b = a + 0.0;


来源:https://stackoverflow.com/questions/51345345/why-cant-i-convert-a-number-into-a-double

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!