如何在扑扑的POST请求中将Map as String发送为String?

时间:2019-06-12 19:11:25

标签: json api post flutter dart

有一个现成的后端和一个工作请求示例。但是我无法使用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\"}]"
}

但是在控制台中,我看到的数据是:

{"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 个答案:

答案 0 :(得分:1)

尝试使用以下数据编码数据:来自软件包json.encode(yourMap)的{​​{1}}