' 不是类型 'List 的子类型',json,api,flutter,rest,dart"/>

首页 > 解决方案 > 获取问题:'_InternalLinkedHashMap' 不是类型 'List 的子类型'

问题描述

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

提取问题:“_InternalLinkedHashMap<String, dynamic>”不是“List”类型的子类型

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

模型.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
    }
  ]
}

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

标签: jsonapiflutterrestdart

解决方案


  • 改变这个:

    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 您将传递一个 type 的对象List<dynamic>,但在您发布的 json"checkpoint": {中,它不是列表,而是地图。但即便如此,这张地图也只有一个关卡。

  • 回答你的最后一个问题

我想访问检查点项目,然后是资源项目(我认为与检查点的过程相同)。

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

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

现在,你应该准备好了。


推荐阅读