flutter - 如何修复 _castError 列表' 不是类型转换中类型 'String' 的子类型
问题描述
我收到此错误列表'不是类型转换中'String'类型的子类型,目前我正在正文中发送表单数据,因为服务器不接受任何原始请求
Future orderConfirmation(CartConfirmationModel cd, orderNotifyData) {
var dataMap = new Map<dynamic, dynamic>();
var orders = [];
for (var item in orderNotifyData.items) {
var productMap = {
'product_id': item.productId,
'qty': item.qty,
};
orders.add(productMap);
}
dataMap['customer_id'] = cd.customerId;
**//dataMap['orderLine'] = orders; // if i add orders to dataMap i get this error**
return http
.post(API + '/orders/create', headers: key, body: dataMap)
.then((data) {
if (data.statusCode == 200) {
final jsonData = json.decode(data.body);
return jsonData['Result'];
}
});
}
// 服务器响应,表示服务器成功接收并返回状态 200
Map (2 items)
1:"Result" -> Map (1 item)
key:"Code"
value:200
0:"Code" -> 200
解决方案
您没有对请求的正文进行编码。
Future orderConfirmation(CartConfirmationModel cd, orderNotifyData) {
var dataMap = new Map<dynamic, dynamic>();
var orders = [];
for (var item in orderNotifyData.items) {
var productMap = {
'product_id': item.productId,
'qty': item.qty,
};
orders.add(productMap);
}
dataMap['customer_id'] = cd.customerId;
dataMap['orderLine'] = orders;
return http
.post(API + '/orders/create', headers: key, body: jsonEncode(dataMap))
.then((data) {
if (data.statusCode == 200) {
final jsonData = json.decode(data.body);
return jsonData['Result'];
}
});
}
推荐阅读
- javascript - 如何将 iframe 中的值发送到 div 元素?
- git - 如何在没有 --force 的情况下将代码上传到 bitbucket
- javascript - Cloud-Firestore 功能在达到有限数量后停止对集合/文档的更改?
- r - 在列表中的向量末尾添加字符串时遇到问题
- node.js - 如何根据函数的结果在 Mongo 中排序?
- c++ - 在类中声明后如何修改 char 数组?
- c++ - 在 C++ 函数中使用函数成员
- vue.js - 删除列表中的项目时,我希望该项目在淡出时保持原位
- flutter - 如何使用提供程序检测数组元素的变化?
- reactjs - 当状态改变时,对文本字段做出反应 onChange 更新整个组件