【问题标题】:Exception occured: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic>' in type cast发生异常:类型“_InternalLinkedHashMap<String, dynamic>”不是类型转换中“List<dynamic>”类型的子类型
【发布时间】:2019-03-20 05:55:17
【问题描述】:

您好,我正在学习颤振并从 api 获取 json,但是在我的模型中对其进行解码时,我收到了下面列出的错误。我知道这与我的分页有关,因为位置结果很好,但无法弄清楚我需要做什么来修复分页结果。我尝试了一堆教程,但没有运气。谁能指出我正确的方向以解决此问题?提前致谢

错误

Exception occured: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic>' in type cast*

location.dart

class Locations {
  final String id;
  final String name;
  final String street;
  final String city;
  final String state;

  Locations(
    {
      this.id,
      this.name,
      this.street,
      this.city,
      this.state,
    }
  );

  Locations.fromJSON(Map<String, dynamic> json)
    : id = json['id'],
      name = json['name'],
      street = json['street'],
      city = json['city'],
      state = json['state'];
}

class Pagination {
  final int pages;
  final int totalResults;

  Pagination(this.pages, this.totalResults);

  Pagination.fromJSON(Map<String, dynamic> json)
    : pages = json['pages'],
      totalResults = json['count'];
}

class LocationsResponse {
  final List<Locations> results;
  final List<Pagination> pagination;
  final String error;

  LocationsResponse(this.results, this.pagination, this.error);

  LocationsResponse.fromJSON(Map<String, dynamic> json)
    : results = (json["results"] as List).map((i) => new Locations.fromJSON(i)).toList(),
      pagination = (json['pagination'] as List).map((i) => new Pagination.fromJSON(i)).toList(),
      error = "";

  LocationsResponse.withError(String errorValue)
      : results = List(),
        pagination = List(),
        error = errorValue;
}

返回示例

{  
   results:[  
      {  
         id:1,
         name:House,
         street:1234 This Street,
         city:Example City,
         state:Ca      
      }
   ],
   pagination:{  
      count:1,
      pages:0
   }
}

【问题讨论】:

    标签: json flutter


    【解决方案1】:

    paginationJSONObject 不是 JSONArray。 像这样更改代码:

     LocationsResponse.fromJSON(Map<String, dynamic> json)
        : results = (json["results"] as List).map((i) => new Locations.fromJSON(i)).toList(),
          pagination = Pagination.fromJSON(json['pagination']),
          error = "";
    

    【讨论】:

    • 感谢您的帮助,当我这样做时,我收到错误初始化程序分页无法分配给字段类型 List
    • 我明白了。我改变了最终的 List 分页;到最后的分页,一切似乎都运行良好!谢谢
    猜你喜欢
    • 1970-01-01
    • 2021-10-15
    • 2021-06-23
    • 2022-08-16
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    • 2021-01-30
    • 1970-01-01
    相关资源
    最近更新 更多