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

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

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

问题

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

  1. Future<void> setData(List<ModelOrder>) async {
  2. var formData = FormData.fromMap({
  3. 'UserId': ModelOrder.userId,
  4. 'ProductId': ModelOrder.productId,
  5. 'quantity': ModelOrder.quantity,
  6. 'productPrice': ModelOrder.price,
  7. 'paymentMethod': ModelOrder.paymentMethod,
  8. });
  9. try {
  10. String url = "example.com/api";
  11. final response = await Dio().post(url, data: formData);
  12. if (response.data == "success") {
  13. print("this is success insert of order");
  14. } else {
  15. print('fail to insert order');
  16. }
  17. } catch (e) {
  18. print(e.toString());
  19. }
  20. }

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

英文:

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

  1. Future&lt;void&gt; setData(List&lt;ModelOrder&gt;) async
  2. {
  3. var formData = FormData.fromMap({
  4. &#39;UserId&#39;: ModelOrder.userId,
  5. &#39;ProductId&#39;: ModelOrder.productId,
  6. &#39;quantity&#39;: ModelOrder.quantity,
  7. &#39;productPrice&#39;: ModelOrder.price,
  8. &#39;paymentMethod&#39;: ModelOrder.paymentMethod,
  9. });
  10. try{
  11. String url = &quot;example.com/api&quot;;
  12. final response = await Dio().post(url,data: formData,);
  13. if(response.data == &quot;success&quot;){
  14. print(&quot;this is success insert of order&quot;);
  15. }
  16. else{
  17. print(&#39;fail to insert order&#39;);
  18. }
  19. }
  20. catch(e){
  21. print(e.toString());
  22. }
  23. }

答案1

得分: 0

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

尝试这样做。

  1. Future<void> setData(List<ModelOrder> data) async {
  2. String url = "example.com/api";
  3. var params = data.map((e) {
  4. return {
  5. 'UserId': e.userId,
  6. 'ProductId': e.productId,
  7. 'quantity': e.quantity,
  8. 'productPrice': e.price,
  9. 'paymentMethod': e.paymentMethod,
  10. };
  11. });
  12. try {
  13. final response = await Dio().post(
  14. url,
  15. data: params,
  16. );
  17. if (response.data == "success") {
  18. print("this is success insert of order");
  19. } else {
  20. print('fail to insert order');
  21. }
  22. } catch (e) {}
  23. }

注意:上述代码是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 -->

  1. Future &lt;void&gt; setData(List&lt;ModelOrder&gt; data) async {
  2. String url = &quot;example.com/api&quot;;
  3. var params = data.map((e) {
  4. return {
  5. &#39;UserId&#39;: e.userId,
  6. &#39;ProductId&#39;: e.productId,
  7. &#39;quantity&#39;: e.quantity,
  8. &#39;productPrice&#39;: e.price,
  9. &#39;paymentMethod&#39;: e.paymentMethod,
  10. };
  11. });
  12. try {
  13. final response = await Dio().post(
  14. url,
  15. data: params,
  16. );
  17. if (response.data == &quot;success&quot;) {
  18. print(&quot;this is success insert of order&quot;);
  19. } else {
  20. print(&#39;fail to insert order&#39;);
  21. }
  22. } catch (e) {}
  23. }

<!-- 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:

确定