繁体   English   中英

我正在尝试创建一个 map,其位置键具有双精度列表作为坐标,但出现错误

[英]I'm trying to create a map with a key of location that has a list of doubles as coordinates but I'm getting an error

我正在尝试创建一个注册 model 并将其发送到我的 API 但“位置”键有另一个包含“类型”的地图/json对象:“点”和“坐标”:[双,双]。

最终的 json object 应该看起来像这样

{
    "name": "Arsh Bansal",
    "email": "ab@yahoo.com",
    "password": "123456789",
    "birthday": "06-21-2000",
    "gender": "Male",
    "location": {
        "type": "Point", 
        "coordinates": [13.0987, 88.403]
    },
    "phone_number": "123456789"
}

我得到的错误是: Unhandled Exception: type '_InternalLinkedHashMap<String, List>' is not a subtype of type 'String' in type cast

用这个

class Location {
  String type;
  List<double> coordinates;

  Location({this.type, this.coordinates});

  Location.fromJson(Map<String, dynamic> json) {
    type = json['type'];
    coordinates = json['coordinates'].cast<double>();
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['type'] = this.type;
    data['coordinates'] = this.coordinates;
    return data;
  }
}

暂无
暂无

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

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