Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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();
like image 621
kenwen Avatar asked Jul 15 '18 04:07

kenwen


People also ask

Can you turn an int into a double?

double c = a; Here, the int type variable is automatically converted into double . It is because double is a higher data type (data type with larger size) and int is a lower data type (data type with smaller size). Hence, there will be no loss in data while converting from int to double .

How do you change an int to a double in darts?

To convert an int to a double , use toDouble() method. The result is an integer with one decimal point ( . 0 ). The result is a double with one decimal point ( .

How do you convert an int to a double in Python?

You need to cast one of the terms to a floating point value. Either explicitly by using float (that is ratio = float(a)/b or ratio=a/float(b) ) or implicitly by adding 0.0 : ratio = (a+0.0)/b . If using Python 2 you can from __future__ import division to make division not be the integral one but the float one.


2 Answers

You can Parse the data Like given below:

Here document is a Map<String,dynamic>

double opening = double.tryParse(document['opening'].toString());
like image 155
Dishant Mahajan Avatar answered Oct 16 '22 08:10

Dishant Mahajan


You can do it in one line:

 double weight = (json['weight'] as num).toDouble();
like image 26
Mi Be Avatar answered Oct 16 '22 09:10

Mi Be