【发布时间】:2021-08-29 06:04:33
【问题描述】:
调试代码时出现此错误
类型“List”不是类型转换中“String”类型的子类型)
我不愿意在正文中添加 json.encode(data),那是因为 服务器不将请求视为原始,所以我必须将正文指定为 form-data。
请帮忙
class CartConfirmatinoAPi {
static const API = kDevelopmentMobile;
static const key = {
'KEY': "***************************",
"Content-Type": "application/x-www-form-urlencoded"
};
Future orderConfirmation(CartConfirmationModel cd, orderNotifyData) {
var dataMap = new Map<String, dynamic>();
final List 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; **// this is the line is causing the problem**
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'];
}
});
}
}
API 要求
$result = [];
$result['customer_id'] = $this->input->post('customer_id', true);
$result['orderLine'] = $this->input->post('orderLine');
$message = [
'Code' => 200,
'Result' => $result ? $result : []
];
$this->response($message,REST_Controller::HTTP_OK);
【问题讨论】:
-
首先尝试打印orders并匹配api要求的格式
-
@PrabhanshuTiwari - 这是我打印订单时的输出
-
[{product_id: 1, qty: 1}, {product_id: 2, qty: 1}, {product_id: 26, qty: 2}]
-
又是什么api需求格式?
-
更新了我的代码,添加了 API requiremtns