如何在颤动的 POST 请求中将 Map 作为字符串发送?
Posted
技术标签:
【中文标题】如何在颤动的 POST 请求中将 Map 作为字符串发送?【英文标题】:How to send Map as String in POST request in flutter? 【发布时间】:2019-10-27 08:44:55 【问题描述】:有一个现成的后端和一个工作请求的示例。但我无法使用 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]
没有\
【问题讨论】:
【参考方案1】:尝试使用包import 'dart:convert';
中的json.encode(yourMap)
对您的数据进行编码
【讨论】:
以上是关于如何在颤动的 POST 请求中将 Map 作为字符串发送?的主要内容,如果未能解决你的问题,请参考以下文章
如何在颤动中将 Map<String, List<object>> 保存在 sharedPreferences 中?
如何在 Swift 3 中使用 Alamofire 在 POST 请求中将给定的 JSON 作为参数发送?