【问题标题】:Problem with fetch: '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic>'提取问题:“_InternalLinkedHashMap<String, dynamic>”不是“List<dynamic>”类型的子类型
【发布时间】:2021-07-03 14:46:43
【问题描述】:

我正在尝试从 API 获取数据,但我不断收到此错误。

获取问题:“_InternalLinkedHashMap”不是“列表”类型的子类型

请告诉我如何修复此代码。

模型.dart

class ComprovanteModel {
  ComprovantesInfoModel jsonResponse;

  String error;

  ComprovanteModel({this.jsonResponse, this.error});

  ComprovanteModel.fromJson(Map<String, dynamic> json)
      : jsonResponse = ComprovantesInfoModel.fromJson(json['json_response']),
        error = '';

  ComprovanteModel.withError(String errorValue)
      : jsonResponse = null,
        error = errorValue;
}

class ComprovanteInfoModel {
  String clientFormalName;
  int volumes;
  int duration;
  CheckpointsModel checkpoint;

  ComprovanteInfoModel({
    this.clientFormalName,
    this.duration,
    this.volumes,
    this.checkpoint,
  });

  ComprovanteInfoModel.fromJson(Map<String, dynamic> json)
      : clientFormalName = json['client_formal_name'],
        checkpoint = CheckpointsModel.fromJson(json['checkpoint']),
        volumes = json['volumes'],
        duration = json['duration'];
}

class CheckpointModel {
  int checkpointId;
  String arrivalTime;
  int status;

  CheckpointModel({
    this.checkpointId,
    this.arrivalTime,
    this.status,
  });

  CheckpointModel.fromJson(Map<String, dynamic> json)
      : checkpointId = json['checkpoint_id'],
        arrivalTime = json['arrival_time'],
        status = json['status'];
}

class CheckpointsModel {
  List<CheckpointModel> checkpoint;

  CheckpointsModel({this.checkpoint});

  CheckpointsModel.fromJson(List<dynamic> jsonList)
      : checkpoint = jsonList.map((e) => CheckpointModel.fromJson(e)).toList();
}

API 响应:

{
  "json_response": [
    {
      "client_formal_name": "",
      "deadline": null,
      "volumes": 1,
      "duration": 5,
      "depot_id": 20,
      "service_id": 109856,
      "georef_provider": "ap_geocoder",
      "checkpoint": {
        "checkpoint_id":,
        "arrival_time": "",
        "duration":,
        "status": 1,
        "event_id": 5,
        "resources": [
          {
            "content_type": "PHOTO",
            "service_event_effect_id": 58,
            "content": "em+ndG6XtE2unp",
            "content_label": "",
            "user_effect_unique_code": ""
          },
          {
            "content_type": "RECEPTOR_INFO",
            "service_event_effect_id": 61,
            "content": "{\"user_relationship_unique_code\":\"\",\"is_expected_receiver\":\"true\",\"document\":\"65979973000240\",\"name\":\"",\"description\":\"",\"id\":\"1\"}",
            "content_label": "",
            "user_effect_unique_code": "2"
          }
        ],
        "event_description": "",
        "operation_date": "",
        "obs": "",
        "is_assistant": false,
        "image": "{\"description\": \"Documento\", \"photo\": \""}"
      },
      "final_attendance_window_b": null
    }
  ]
}

我想访问检查点项,然后是资源项(我认为它与检查点的过程相同)。我正在使用列表,但我认为不正确,我想使用地图,但我不知道如何使用。请告诉我一个方法。

【问题讨论】:

    标签: json api flutter rest dart


    【解决方案1】:
    • 改变这个:

      ComprovanteModel.fromJson(Map<String, dynamic> json)
          : jsonResponse = ComprovantesInfoModel.fromJson(json['json_response']),
            error = '';
      

    到这里:

     ComprovanteModel.fromJson(Map<String, dynamic> json)
          : jsonResponse = ComprovantesInfoModel.fromJson(json['json_response'][0]), //added [0] here.
            error = '';
    
    • 如果您仔细查看您的回复,它确实有您需要的地图,但该地图实际上是在一个列表中,请注意 "json_response": [ 中 {} 周围的方括号 []。

    您需要访问的地图位于此列表的 index[0] 处,然后一切正常。

    第二件事,这个:

     CheckpointsModel.fromJson(List<dynamic> jsonList)
          : checkpoint = jsonList.map((e) => CheckpointModel.fromJson(e)).toList();
    }
    
    • 您告诉 Flutter 您将传递一个 List&lt;dynamic&gt; 类型的对象,但在您发布的 json 中,"checkpoint": { 不是列表,而是地图。但即便如此,这张地图也只有一个关卡。

    • 回答你的最后一个问题

    我想访问检查点项目,然后是资源项目(我 think 和 checkpoint 是同一个过程)。

    "resources": [ 确实是一个地图列表。在您的代码中,您没有发布资源模型,但我假设您想要 List&lt;Resources&gt; 而不是 List&lt;CheckPoint&gt;,它看起来像这样:

    class SingleResourceModel {
       String contentType;
       int serviceId;
       String content;
       String contentLabel;
       String uniqueCode;
    
      SingleResourceModel({
        this.contentType,
        this.serviceId,
        this.content,
        this.contentLabel,
        this.uniqueCode
      });
    
      SingleResourceModel.fromJson(Map<String, dynamic> json)
          : contentType = json['content_type'],
            serviceId = json['service_event_effect_id'],
            content = json['content'];
            contentLabel = json['content_label'],
            uniqueCode = json['user_effect_unique_code'];
    }
    class ListResourceModel {
      List<SingleResourceModel> resourcesList;
    
      ListResourceModel({this.resourcesList});
    
      ListResourceModel.fromJson(List<Map<String, dynamic>> jsonList)
          : resourcesList = jsonList.map((e) => SingleResourceModel.fromJson(e)).toList();
    }
    

    最后,你可以修改你的CheckPoint模型,并添加一个ListResourceModel,最终看起来像这样:

    class CheckpointModel {
      int checkpointId;
      String arrivalTime;
      int status;
      ListResourceModel resourcesList;
    
      CheckpointModel({
        this.checkpointId,
        this.arrivalTime,
        this.status,
        this.resourcesList
      });
    
      CheckpointModel.fromJson(Map<String, dynamic> json)
          : checkpointId = json['checkpoint_id'],
            arrivalTime = json['arrival_time'],
            status = json['status'],
            resourcesList= json['resources'];    
       }
    

    现在,你应该准备好了。

    【讨论】:

      猜你喜欢
      • 2020-07-03
      • 2019-04-03
      • 2021-01-30
      • 2019-01-26
      • 2021-01-13
      • 2022-11-11
      • 2022-07-21
      • 2019-05-02
      • 2021-01-22
      相关资源
      最近更新 更多