简体   繁体   中英

How to send Map as String in POST request in flutter?

There is a ready-made backend and an example of a working request. But I cannot send a correct post request using dio or 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();
}

I need to send the data as:

{"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\"}]"
}

but in cosole i see that data is:

{"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}]}

without \\

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM