我正在尝试返回此api https://api.maisdecristo.com/api/parents/mdcget00_parentkids/48984974812中的项
但总是错误
未处理的异常:类型"List"不是类型"Map〈String,dynamic〉"的子类型
这是我的目标和模型:
Future<ProdutoModel> getProduto() async {
try {
final response = await http.get(Uri.parse(
"https://api.maisdecristo.com/api/parents/mdcget00_parentkids/48984974812"));
var res = jsonDecode(response.body);
print(res);
_accountListModel = ProdutoModel.fromJson(res);
var data = res['filhos'] as List;
setState(() {
_list =
data.map<FilhoModel>((json) => FilhoModel.fromJson(json)).toList();
});
return _accountListModel;
} catch (e) {
rethrow;
}
}
class ProdutoModel {
ProdutoModel({
required this.phone,
required this.desperson,
required this.desemail,
required this.filhos,
});
final String phone;
final String desperson;
final String desemail;
final List<FilhoModel> filhos;
factory ProdutoModel.fromJson(Map<String, dynamic> json) => ProdutoModel(
phone: json["phone"],
desperson: json["desperson"],
desemail: json["desemail"],
filhos: List<FilhoModel>.from(
json["filhos"].map((x) => FilhoModel.fromJson(x))),
);
}
class FilhoModel {
FilhoModel({
required this.age,
required this.firstname,
required this.lastname,
});
final int age;
final String firstname;
final String lastname;
factory FilhoModel.fromJson(Map<String, dynamic> json) => FilhoModel(
age: json["age"],
firstname: json["firstname"],
lastname: json["lastname"],
);
}
返回this api
3条答案
按热度按时间wwodge7n1#
返回的位于List,因此您必须执行以下操作:
cunj1qz12#
您有类型转换问题,请确保来自api的类型与模型相同
eufgjt7s3#
你可以使用这个网站:https://app.quicktype.io
生成任何json文件的模型,而不会出现任何错误,并且它可以从json和json获取函数