简体   繁体   English

从 Flutter 和 Dart 中解析来自 JSON 的双精度值

[英]Parsing double values from JSON in Flutter and Dart

I have problems while I try to get double values from JSON in Flutter.当我尝试从 Flutter 中的 JSON 获取双精度值时遇到问题。

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' Dart 错误:未处理的异常:类型“双”不是“字符串”类型的子类型

I tried some to user double.parse(json["lng"]) instead but got same error.我尝试了一些用户double.parse(json["lng"]) ,但得到了同样的错误。
Same time this way of getting data from JSON works fine with other types.同时,这种从 JSON 获取数据的方式适用于其他类型。

This is JSON example这是 JSON 示例

{ 
   point: {
     title: "Point title",
     lat: 42.123456,
     lng: 32.26567
  }
}

The Dart JSON parser converts the property inside json and apparently is clever enough to spit a double type. Dart JSON 解析器将属性转换为 json 并且显然足够聪明,可以吐出 double 类型。

someVariable as double expects a String at the left side. someVariable as double需要左侧的字符串。

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".这将涵盖 JSON 中的数据类似于“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.在这种情况下,dart json 转换器会将类型转换为 int,如果您总是期待双精度,它会破坏您的代码。

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

If your lat field is nullable, for example:如果您的lat字段可以为空,例如:

double? lat

In your fromJson method, instead of:在您的 fromJson 方法中,而不是:

lat: json["lat"] as double,

Use:利用:

lat : double.tryParse(json['lat'].toString()),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM