flutter 我如何在dio库中发送表单体中的未来列表

kkbh8khc  于 2023-05-29  发布在  Flutter
关注(0)|答案(1)|浏览(106)

你好家伙,我已经尝试发送Flutter名单,但现在我只能发送一个接一个的数据。我正试图通过dio发送列表在这里我已经尝试

Future<void> setData(List<ModelOrder>) async 
       {
    
        var formData = FormData.fromMap({
                  
                      'UserId': ModelOrder.userId,
                      'ProductId': ModelOrder.productId, 
                      'quantity': ModelOrder.quantity, 
                      'productPrice': ModelOrder.price,
                      'paymentMethod': ModelOrder.paymentMethod,
                     
                });
        try{
            String url = "example.com/api";
    
            final response = await Dio().post(url,data: formData,);
    
           
            if(response.data == "success"){
             print("this is success insert of order");
            } 
            else{
              print('fail to insert order');
    
            }
           
    
        }
        catch(e){
          print(e.toString());
        }
    
        
      }
ws51t4hk

ws51t4hk1#

我认为,你不能使用表单数据类型作为主体发布列表。
试试这个。

Future <void> setData(List<ModelOrder> data) async {
  String url = "example.com/api";
  var params = data.map((e) {
    return {
      'UserId': e.userId,
      'ProductId': e.productId,
      'quantity': e.quantity,
      'productPrice': e.price,
      'paymentMethod': e.paymentMethod,
    };
  });

  try {
    final response = await Dio().post(
      url,
      data: params,
    );

    if (response.data == "success") {
      print("this is success insert of order");
    } else {
      print('fail to insert order');
    }
  } catch (e) {}
}

相关问题