【问题标题】:Unhandled Exception: type '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'Map<String, dynamic>' in dart未处理的异常:类型 '_InternalLinkedHashMap<dynamic, dynamic>' 不是 dart 中类型 'Map<String, dynamic>' 的子类型
【发布时间】:2021-02-11 11:27:03
【问题描述】:

我正在尝试解析 json 响应,但我有这个错误,我不知道为什么。错误发生在 Data 对象内部。因为当我移除对象时,它可以完美运行。我需要知道做错了什么。

class TripDetails {
  String operatorId;
  String title;
  String body;
  Data data;

  TripDetails({this.operatorId, this.title, this.body, this.data});

  TripDetails.fromJson(Map<String, dynamic> json) {
    operatorId = json['operatorId'];
    title = json['title'];
    body = json['body'];
    data = json['data'] != null ? new Data.fromJson(json['data']) : null;
  }

}

class Data {
  String clickAction;
  String tripId;
  String tripType;
  String date;
  String earning;
  String distance;
  String stores;

  Data(
      {this.clickAction,
        this.tripId,
        this.tripType,
        this.date,
        this.earning,
        this.distance,
        this.stores});

  Data.fromJson(Map<String, dynamic> json) {
    clickAction = json['click_action'];
    tripId = json['tripId'];
    tripType = json['trip_type'];
    date = json['date'];
    earning = json['earning'];
    distance = json['distance'];
    stores = json['stores'];
  }

这是要解析的数据。问题发生在数据对象内部。

{
  "operatorId": "W5u8Bz2oeIMM32I8ZSanreKvwLk2",
  "title": "Hoole",
  "body": "New Trip Request",
  "data": {
    "click_action": "FLUTTER_NOTIFICATION_CLICK",
    "tripId": "12131131",
    "trip_type": "trip",
    "date":"12343325",
    "earning":"123.54",
    "distance":"123",
    "stores":"[{\"id\":\"pnp1\",\"name\":\"Pick n Pay\",\"logo\":\"https://example.com\"}, {\"id\":\"woolies1\",\"name\":\"Woolworths\",\"logo\":\"https://example.com\"}]"
  }
}

【问题讨论】:

  • 贴出解析代码

标签: flutter dart


【解决方案1】:

改变这个:

Data.fromJson(Map<String, dynamic> json) {

到这里:

Data.fromJson(Map json) {

【讨论】:

    猜你喜欢
    • 2020-08-20
    • 2019-08-07
    • 1970-01-01
    • 2021-09-20
    • 2021-07-11
    • 2021-01-17
    • 2018-12-14
    • 2019-04-03
    • 2020-07-14
    相关资源
    最近更新 更多