I have problems while I try to get double values from JSON in Flutter.
class MapPoint {
String title;
double lat;
double lng;
MapPoint({this.title, this.lat, this.lng});
factory MapPoint.fromJson(Map<String, dynamic> json) {
return MapPoint(
title: json["title"] as String,
lat: json["lat"] as double,
lng: json["lng"] as double
);
}
}
For some reason I got error
Dart Error: Unhandled exception: type 'double' is not a subtype of type 'String'
I tried some to user double.parse(json["lng"])
instead but got same error.
Same time this way of getting data from JSON works fine with other types.
This is JSON example
{
point: {
title: "Point title",
lat: 42.123456,
lng: 32.26567
}
}
Step 1: Create a project in Vs code, And remove the default code. Step 2: Before writing the code just add the HTTP plugin in your pubspec yaml file. Step 3: In main. dart file call the main() function , inside it run the runApp( ) method and give it an App (MyApp).
The Dart JSON parser converts the property inside json and apparently is clever enough to spit a double type.
someVariable as double
expects a String at the left side.
Whats probably happening is that you're trying to convert a double into a double.
I would try something like this:
lat: json["lat"].toDouble(),
This would cover the case of the data in your JSON comes like "5". In this case the dart json converter would cast the type to int and it would break your code if you are always expecting a double.
I was having the same problem, the way I think it was this:
class MapPoint {
String title;
double lat;
double lng;
MapPoint({this.title, this.lat, this.lng});
factory MapPoint.fromJson(Map<String, dynamic> json) {
return MapPoint(
title: json["title"] as String,
lat: json["lat"] is int ? (json['lat'] as int).toDouble() : json['lat'],
lng: json["lng"] is int ? (json['lng'] as int).toDouble() : json['lng']
);
}
}
I can not reproduce
void main() {
final json = {
"point": {"title": "Point title", "lat": 42.123456, "lng": 32.26567}
};
final p = MapPoint.fromJson(json);
print(p);
}
class MapPoint {
String title;
double lat;
double lng;
MapPoint({this.title, this.lat, this.lng});
factory MapPoint.fromJson(Map<String, dynamic> json) {
return MapPoint(
title: json["title"] as String,
lat: json["lat"] as double,
lng: json["lng"] as double);
}
}
i can fix this problem this way
class MapPoint {
String title;
double lat;
double lng;
MapPoint({this.title, this.lat, this.lng});
factory MapPoint.fromJson(Map<String, dynamic> json) {
return MapPoint(
title: json["title"] as String,
lat: json["lat"] *1.0,
lng: json["lng"] *1.0
);
}
}
Rewrite your fromJson
method as follows:
factory MapPoint.fromJson(Map<String, dynamic> json) {
return MapPoint(
title: json["title"] as String,
lat : double.parse(json["lat"].toString()),
lng : double.parse(json["lng"].toString()),
);
}
If your lat
field is nullable, for example:
double? lat
In your fromJson
method, instead of:
lat : double.parse(json["lat"].toString()),
Use:
lat : double.tryParse(json["lat"].toString()),
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