gpt4 book ai didi

json - 如何在 Flutter 的 POST 请求中将 Map 作为 String 发送?

转载 作者:IT王子 更新时间:2023-10-29 07:18:25 24 4
gpt4 key购买 nike

有一个现成的后端和一个工作请求的例子。但是我无法使用 dio 或 http.post 发送正确的发布请求。

void postOrder(Datum basket) async{
final prefs = await SharedPreferences.getInstance();
final myUrl = prefs.getString('portal_url') ?? null;

final Map<String, dynamic> productMap = {
"item_id" : basket.id,
"shop_id" : basket.shopId,
"unit_price" : basket.unitPrice,
"discount_percent" : basket.discountPercent,
"name" : basket.name,
"qty" : "1",
"user_id" : "6",
"payment_trans_id" : "",
"delivery_address" : "Test adress",
"billing_address" : "Test adress 2",
"total_amount" : 10,
"basket_item_attribute" : "",
"basket_item_attribute_id" : "",
"payment_method" : "cod",
"email" : "test@test.com",
"phone" : "+77777777777",
"coupon_discount_amount" : "0.0",
"flat_rate_shipping" : "25",
"platform" : "IOS"
};
try {
Response response = await Dio().post('$myUrl/transactions/add', data: {
"orders": productMap
},
);
if (response.statusCode == 200){
print('Response code - ${response.statusCode}');
print("Response body: ${response.data}");
print("Send body: $productMap");
}
} catch (e) {
print(e);
}
notifyListeners();
}

我需要将数据发送为:

{"orders":"[{\"item_id\":\"41\",\"shop_id\":\"1\",\"unit_price\":\"5.0\",\"discount_percent\":\"\",\"name\":\"Test product 1  \",\"qty\":1,\"user_id\":7,\"payment_trans_id\":\"\",\"delivery_address\":\"test adress1\",\"billing_address\":\"test adress 2\",\"total_amount\":10,\"basket_item_attribute_id\":\"\",\"basket_item_attribute\":\"\",\"payment_method\":\"cod\",\"email\":\"test@test.com\",\"phone\":\"+77777777777\",\"coupon_discount_amount\":\"0.0\",\"flat_rate_shipping\":\"25\",\"platform\":\"Android\"}]"
}

但在 cosole 中我看到数据是:

{"orders": [{item_id: 1, shop_id: 1, unit_price: 10, discount_percent: , name: Sushi Rolls A, qty: 1, user_id: 6, payment_trans_id: , delivery_address: Test adress, billing_address: Test adress 2, total_amount: 1, basket_item_attribute: , basket_item_attribute_id: , payment_method: cod, email: test@test.com, phone: +77777777777, coupon_discount_amount: 0.0, flat_rate_shipping: 25, platform: IOS}]}

没有\

最佳答案

尝试使用以下代码对您的数据进行编码:json.encode(yourMap) from package import 'dart:convert';

关于json - 如何在 Flutter 的 POST 请求中将 Map 作为 String 发送?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56568919/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com