【发布时间】:2021-04-13 00:45:30
【问题描述】:
所以我创建了一个应用程序来通过 api 读取数据并尝试解析 JSON api
这是我的 Error screenshot
我尝试将其更改为列表,但它仍然读取错误
这是我的代码 elephant.dart
@JsonSerializable()
class ElephantList{
ElephantList({this.biodata});
final List<Elephant> biodata;
factory ElephantList.fromJson(Map<String, dynamic> json) => _$ElephantListFromJson(json);
Map<String, dynamic> toJson() => _$ElephantListToJson(this);
}
@JsonSerializable()
class Elephant{
Elephant({this.name, this.affiliation, this.species,
this.sex, this.fictional, this.dob, this.dod, this.wikilink, this.image, this.note});
//final String index;
final String name;
final String affiliation;
final String species;
final String sex;
final String fictional;
final String dob;
final String dod;
final String wikilink;
final String image;
final String note;
factory Elephant.fromJson(Map<String, dynamic> json) => _$ElephantFromJson(json);
Map<String, dynamic> toJson() => _$ElephantToJson(this);
}
Future<ElephantList> getElephantList() async{
const url = 'https://elephant-api.herokuapp.com/elephants/sex/male';
final response = await http.get(url);
if(response.statusCode == 200){
return ElephantList.fromJson(json.decode(response.body));
}else{
throw HttpException('Error ${response.reasonPhrase}', uri: Uri.parse(url));
}
}
如何纠正这个错误? 请帮忙
【问题讨论】:
-
你能提供
response.body的输出吗?