<字符串,动态>"不是类型"List< dynamic>"的子类型扑
问题描述:
这是我的示例json响应
Here is my sample json response
{
"billing": [
{
"id": 1,
"add_id": 23
}
],
"shipping": [
{
"id": 2,
"add_id": 345
},
{
"id": 3,
"add_id": 345
}
]
}
这是我的api接收部分
Here is my api receiving part
List responseList = response.data['data'];//getting api response after decode
List<AddressListGroupModel> listData =
responseList.map((f) => AddressListGroupModel.fromJson(f)).toList();
return listData;
这是我的模特
class AddressListGroupModel {
List<Add> billing;
List<Add> shipping;
AddressListGroupModel({this.billing, this.shipping, this.billingShipping});
AddressListGroupModel.fromJson(Map<String, dynamic> json) {
billing = List<Add>.from(json["billing"].map((x) => Add.fromJson(x)));
shipping = List<Add>.from(json["shipping"].map((x) => Add.fromJson(x)));
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['billing'] = List<dynamic>.from(billing.map((x) => x.toJson()));
data['shipping'] = List<dynamic>.from(shipping.map((x) => x.toJson()));
return data;
}
}
class Add {
int id;
int addId;
Add({
this.id,
this.addId,
});
Add.fromJson(Map<String, dynamic> json) {
id = json["id"];
userId = json["add_id"];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['add_id'] = this.userId;
return data;
}
}
我需要将响应映射到模型,因为响应即将作为对象出现,所以我出错了.那么如何映射此响应..请帮助解决问题
I need to map the response to the model , I am getting error since response is coming as object .. So how can I map this response .. Please help to fix the issue
我在列表responseList = response.data ['data'] 中遇到错误,因为它作为对象出现
I am getting error in List responseList = response.data['data'] since it is coming as a object
答
{ //<-- Map
"billing": [ //<-- List
{ // <-- Map
"id": 1,
"add_id": 23
}
],
"shipping": [// <-- List
{ // <-- Map
"id": 2,
"add_id": 345
},
{
"id": 3,
"add_id": 345
}
]
}
我认为您的响应[数据]
返回一个 Map
,因此您只需替换 Map
而不是 List
.
I think your response[data]
returns a single Map
so you can just replace Map
instead of List
.
Map data = response[data];
AddressListGroupModel model = AddressListGroupModel.fromMap(data);
或者如果它返回 List
.
final data = response[data] as Map;
List<AddressListGroupModel> listData =
responseList.map((f) => AddressListGroupModel.fromMap(f)).toList();
return listData;
这就是您的代码的样子
class AddressListGroupModel {
final List<Add> billing;
final List<Add> shipping;
AddressListGroupModel({
this.billing,
this.shipping,
});
// to know more about factory constructor
// read https://dart.dev/guides/language/language-tour#constructors
factory AddressListGroupModel.fromMap(Map<String, dynamic> map) {
if (map == null) return null;
return AddressListGroupModel(
billing: List<Add>.from(map['billing']?.map((x) => Add.fromMap(x))),
shipping: List<Add>.from(map['shipping']?.map((x) => Add.fromMap(x))),
);
}
Map<String, dynamic> toMap() {
return {
'billing': billing?.map((x) => x?.toMap())?.toList(),
'shipping': shipping?.map((x) => x?.toMap())?.toList(),
};
}
AddressListGroupModel copyWith({
List<Add> billing,
List<Add> shipping,
}) {
return AddressListGroupModel(
billing: billing ?? this.billing,
shipping: shipping ?? this.shipping,
);
}
}
class Add {
final int id;
// assuming this is userId instead of addId.
final int userId;
Add({
this.id,
this.userId,
});
factory Add.fromMap(Map<String, dynamic> map) {
if (map == null) return null;
return Add(
id: map['id'],
userId: map['userId'],
);
}
Map<String, dynamic> toJson() {
return {
'id': id,
'userId': userId,
};
}
}