你好,我正在学习抖动,并从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
}
}
答案 0 :(得分:1)
pagination
是JSONObject
而不是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 = "";