【问题标题】:Error: type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>'错误:类型 'List<dynamic>' 不是类型 'Map<String, dynamic>' 的子类型
【发布时间】: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的输出吗?

标签: json api flutter


【解决方案1】:

api:https://elephant-api.herokuapp.com/elephants/sex/male 返回一个大象列表,格式为:[Elephant , Elephant , ... ],而您指定从该请求接收到的 json 是 Map&lt;String, dynamic&gt;,它的格式为:{ elephants : [ ... ] },所以只需替换每个Map&lt;String, dynamic&gt;List&lt;dynamic&gt;

@JsonSerializable(anyMap: true)
class ElephantList{
  ElephantList({this.biodata});
  final List<Elephant> biodata;
  factory ElephantList.fromJson(List<dynamic> json) => _$ElephantListFromJson(json);
  List<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));
  }
}

编辑@JsonSerializable 预期 json 为 Map&lt;string,dynamic&gt;,因此要更改预期类型,您应该添加 anyMap: true 我相应地更改了代码

【讨论】:

  • 使用 List 更改每个 Map 时会添加类似这样的错误 > 错误:参数类型 'List' 无法分配给参数类型'地图'。 (argument_type_not_assignable 在 [ujian_ptm] lib\elephant.dart:32)
  • 是的,我的错误,如果你可以尝试的话,我用更新的代码编辑了我的答案
猜你喜欢
  • 2019-01-22
  • 2021-07-01
  • 2021-12-29
  • 2023-01-08
  • 2021-10-25
  • 2020-07-03
  • 2019-12-05
  • 2023-03-11
  • 2021-04-06
相关资源
最近更新 更多