【发布时间】:2019-11-22 20:56:27
【问题描述】:
我正在开发一个关于颤振的购物应用程序,我必须将用户信息与购物车中的商品列表一起发送到服务器。我整天在互联网上搜索,找不到适合我的解决方案。我必须提交的 JSON 如下所示。
{
"areaId": 9,
"userId": 4,
"totalOrder": [{
"categoryId": "1",
"itemId": "1",
"priceOfItem": 28,
"serviceTypeId": 1,
"title": "Shalwar Kameez"
}, {
"categoryId": "1",
"itemId": "2",
"priceOfItem": 28,
"serviceTypeId": 1,
"title": "Shalwar Kameez Ladies (2Pcs)"
}, {
"categoryId": "1",
"itemId": "2",
"priceOfItem": 28,
"serviceTypeId": 1,
"title": "Shalwar Kameez Ladies (2Pcs)"
}]
}
我向服务器发送数据的方式如下。
Future<String> saveOrder() async {
var map = new Map<String, dynamic>();
map["userId"] = await SharedPreferencesUser.getUserId();
map["areaId"] = await SharedPreferencesUser.getAreaId();
map["totalOrder"] = cart.items; // this is list of items in cart List<CartItem>
var res = await http.post(
Uri.encodeFull(Url.url + Apis.saveOrder),
headers: {"Accept": "application/json"},
body: json.encode(map), //encoding to json
);
return res.body;
}
我收到的异常是 “未处理的异常:将对象转换为可编码对象失败:'CartItem' 的实例”
CartItem 类看起来像这样
class CartItem {
String id;
String categoryId;
String itemName;
int piece;
int price;
int quantity;
String serviceType;
String serviceId;
String defalutCategory;
CartItem(this.id, this.categoryId, this.itemName, this.piece, this.price,
this.quantity, {this.serviceType, this.serviceId, this.defalutCategory});
}
请帮我解决这个问题。谢谢
【问题讨论】: