英文:
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<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());
}
}
答案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 <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) {}
}
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论