如何在dio库中以表单主体发送未来的列表

huangapple go评论55阅读模式
英文:

How do I send future list in form body in dio library

问题

你好,我已经为您翻译了代码部分,如下所示:

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());
  }
}

希望这对您有帮助。如果您有任何其他问题,请随时提问。

英文:

Hello Guy I have tried to send the flutter list but now I am only able to send one by one data. I am trying to send list through dio here what I have tried

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

答案1

得分: 0

我认为,你不能使用form-data类型作为请求体来发布列表。

尝试这样做。

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

注意:上述代码是Dart语言的示例代码,用于将列表数据发送到指定的URL。

英文:

I think, you cant post list using form-data type as a body.

try this.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

Future &lt;void&gt; setData(List&lt;ModelOrder&gt; data) async {
  String url = &quot;example.com/api&quot;;
  var params = data.map((e) {
    return {
      &#39;UserId&#39;: e.userId,
      &#39;ProductId&#39;: e.productId,
      &#39;quantity&#39;: e.quantity,
      &#39;productPrice&#39;: e.price,
      &#39;paymentMethod&#39;: e.paymentMethod,
    };
  });

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

    if (response.data == &quot;success&quot;) {
      print(&quot;this is success insert of order&quot;);
    } else {
      print(&#39;fail to insert order&#39;);
    }
  } catch (e) {}
}

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月24日 22:24:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76324599.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定