问题
Here is my sample json response
{
"billing": [
{
"id": 1,
"add_id": 23
}
],
"shipping": [
{
"id": 2,
"add_id": 345
},
{
"id": 3,
"add_id": 345
}
]
}
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;
Here is my model
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
I am getting error in List responseList = response.data['data'] since it is coming as a object
回答1:
You are trying to map an array when your response is a json. You must use this variable for doing the mapping.
Map<String, dynamic> resp = response.data;
AddressListGroupModel addressListGroupModel = AddressListGroupModel.fromJson(resp);
Because your json only gives one value and not a list of addresses.
回答2:
You cannot automatically add the Json output to the list. Use this;
AddressListGroupModel.fromJson(response.data);
回答3:
{ //<-- Map
"billing": [ //<-- List
{ // <-- Map
"id": 1,
"add_id": 23
}
],
"shipping": [// <-- List
{ // <-- Map
"id": 2,
"add_id": 345
},
{
"id": 3,
"add_id": 345
}
]
}
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);
or incase it returns a List.
final data = response[data] as Map;
List<AddressListGroupModel> listData =
responseList.map((f) => AddressListGroupModel.fromMap(f)).toList();
return listData;
This is how Your code should look like
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,
};
}
}
来源:https://stackoverflow.com/questions/65796626/string-dynamic-is-not-a-subtype-of-type-listdynamic-flutter