17

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
  }
}
7
  • What JSON causes this error? If your values are wrapped in quotes, they won't be interpreted as double. Commented Jan 20, 2019 at 14:07
  • JSON is fine because I can get all other values (sting and int) from it Commented Jan 20, 2019 at 14:09
  • 1
    Either the JSON doesn't fit your code or your code doesn't fit your JSON. Without providing the JSON that causes the error you can as well delete the question because it just doesn't provide the required information to diagnose the issue. Commented Jan 20, 2019 at 14:11
  • I added example of JSON. One thing that can be unusual is that double values are not presented as strings. Commented Jan 20, 2019 at 14:17
  • That is not valid JSON. In JSON keys must be quoted and there is a , missing after "Point title". Double values are never represented as String in JSON. Commented Jan 20, 2019 at 14:19

5 Answers 5

30

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.

Sign up to request clarification or add additional context in comments.

2 Comments

lat: double.parse(json["lat"])
If you are not sure whethe rthe type is int or double; better to declare it as num
8

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']
    );
  }
}

1 Comment

Thanks a lot. I have tried several ways, but this works :D
6

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()),

1 Comment

Hello @moonvader, please do consider on marking the answer as accepted, if it has helped to solve your problem (or to answer your question).
2

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);
  }
}

Comments

2

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
    );
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.