Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing double values from JSON in Flutter and Dart

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
  }
}
like image 599
moonvader Avatar asked Jan 20 '19 14:01

moonvader


People also ask

How do you get data from a JSON format in Flutter?

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).


5 Answers

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.

like image 68
Gilson Cavalcanti Avatar answered Oct 17 '22 22:10

Gilson Cavalcanti


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']
    );
  }
}
like image 40
Matheus Toniolli Avatar answered Oct 17 '22 21:10

Matheus Toniolli


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);
  }
}
like image 31
Günter Zöchbauer Avatar answered Oct 17 '22 21:10

Günter Zöchbauer


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
    );
  }
}
like image 2
Uditha Chathura Banadara Avatar answered Oct 17 '22 22:10

Uditha Chathura Banadara


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()),
like image 2
Alex Correia Avatar answered Oct 17 '22 20:10

Alex Correia