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();
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 .
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 ( .
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.
You can Parse the data Like given below:
Here document is a Map<String,dynamic>
double opening = double.tryParse(document['opening'].toString());
You can do it in one line:
double weight = (json['weight'] as num).toDouble();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With