dart 如何在dio flutter中发布模型类的未来列表

ffscu2ro  于 2023-05-26  发布在  Flutter
关注(0)|答案(1)|浏览(135)

大家好,我想发布我的模型类列表的数据,我尝试过,但它不工作,希望你们能帮助我,这是我已经尝试过的

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");
            } //if already a user
            else{
              print('fail to insert order');
    
            }
           
    
        }
        catch(e){
          // print(e.toString());
        }
    
        
      }
apeeds0o

apeeds0o1#

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

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) {}
}

相关问题