gpt4 book ai didi

json - 发生异常 : type '_InternalLinkedHashMap' is not a subtype of type 'List' in type cast

转载 作者:IT王子 更新时间:2023-10-29 07:21:49 25 4
gpt4 key购买 nike

您好,我正在学习 flutter 并从 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
}
}

最佳答案

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 = "";

关于json - 发生异常 : type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic>' in type cast,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55254379/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com