英文:
Sending a list of images along with data in http post flutter
问题
我有一组图像和数据(字符串),需要发送到我的后端。这是重要的函数:
Future MakePostPetOfferRequest(
{required String title,
required String description,
required String price,
required String city,
required String species,
required String sex,
required String ageYears,
required String ageMonths,
required String breed,
required List<File?> images}) async {
Uri url = Uri.parse('http://$ip/create_sale_post');
Map<String, dynamic> body = {
"Title": title,
"Description": description,
"City": city,
"Species": species,
"Price": price,
"Sex": sex,
"Years": ageYears,
"Months": ageMonths,
"Breed": breed,
"images": images,
};
var response = await http.post(url, headers: {"cookie": sessionCookie}, body: body);
if (response.headers['set-cookie'] != null) {
String initSessionCookie = response.headers['set-cookie']!;
if (sessionCookie != "") sessionCookie = initSessionCookie.substring(0, initSessionCookie.indexOf(";"));
}
return response;
}
但是我遇到了不同的错误,首先是错误:
类型 'List<File?>' 不是 'String' 类型的子类型
我也不确定如何将图像添加到一个单一字段,就像在 Postman 中这样做:
希望问题清楚,感谢您的帮助。
英文:
I have a list of images and data (string) that I need to send to my backend.
This is the important function:
Future MakePostPetOfferRequest(
{required String title,
required String description,
required String price,
required String city,
required String species,
required String sex,
required String ageYears,
required String ageMonths,
required String breed,
required List<File?> images}) async {
Uri url = Uri.parse('http://$ip/create_sale_post');
Map<String, dynamic> body = {
"Title": title,
"Description": description,
"City": city,
"Species": species,
"Price": price,
"Sex": sex,
"Years": ageYears,
"Months": ageMonths,
"Breed": breed,
"images": images,
};
var response = await http.post(url, headers: {"cookie": sessionCookie}, body: body);
if (response.headers['set-cookie'] != null) {
String initSessionCookie = response.headers['set-cookie']!;
if (sessionCookie != "") sessionCookie = initSessionCookie.substring(0, initSessionCookie.indexOf(";"));
}
return response;
}
But I am faced with different errors, the first is the error:
>type 'List<File?>' is not a subtype of type 'String' in type cast
I also am not sure how I can add the images to one single field, like I do it on Post man here:
I hope the question is clear and thank you for the assistance.
答案1
得分: 2
要上传图片,你应该使用表单数据(formdata)。
import 'package:dio/dio.dart' as formDataMap;
formDataMap.FormData formData = formDataMap.fromMap({
"Title": title,
"Description": description,
"City": city,
"Species": species,
"Price": price,
"Sex": sex,
"Years": ageYears,
"Months": ageMonths,
"Breed": breed
});
for (var file in images) {
formData.files.addAll([
MapEntry("images[]", await formDataMap.MultipartFile.fromFile(file.path)),
]);
}
这段代码用于创建表单数据,以便上传图像。
英文:
To post image you should use a formdata.
import 'package:dio/dio.dart' as formDataMap;
formDataMap.FormData formData = formDataMap.fromMap({
"Title": title,
"Description": description,
"City": city,
"Species": species,
"Price": price,
"Sex": sex,
"Years": ageYears,
"Months": ageMonths,
"Breed": breed
});
for (var file in images) {
formData.files.addAll([
MapEntry("images[]",
await formDataMap.MultipartFile.fromFile(file.path)),
]);
}
答案2
得分: 0
我已找到一个方法来实现这个,而且它非常简单,只使用了内置的 *http.MultipartRequest()* 方法。
Uri url = Uri.parse('http://$ip/create_sale_post');
var request = http.MultipartRequest('POST', url);
for (var i = 0; i < images.length; i++) {
var image = await http.MultipartFile.fromPath(
'images',
images[i]!.path,
);
request.files.add(image);
}
request.headers['cookie'] = sessionCookie;
request.fields["Title"] = title;
request.fields["Description"] = description;
request.fields["City"] = city;
request.fields["Species"] = species;
request.fields["Price"] = price;
request.fields["Sex"] = sex;
request.fields["Years"] = ageYears;
request.fields["Months"] = ageMonths;
request.fields["Breed"] = breed;
request.headers['Content-Type'] = 'multipart/form-data';
var response = await request.send();
<details>
<summary>英文:</summary>
I have found a way to do it, and it is fairly simple, using no third party packages, only the built in *http.MultipartRequest()* method.
Uri url = Uri.parse('http://$ip/create_sale_post');
var request = http.MultipartRequest('POST', url);
for (var i = 0; i < images.length; i++) {
var image = await http.MultipartFile.fromPath(
'images',
images[i]!.path,
);
request.files.add(image);
}
request.headers['cookie'] = sessionCookie;
request.fields["Title"] = title;
request.fields["Description"] = description;
request.fields["City"] = city;
request.fields["Species"] = species;
request.fields["Price"] = price;
request.fields["Sex"] = sex;
request.fields["Years"] = ageYears;
request.fields["Months"] = ageMonths;
request.fields["Breed"] = breed;
request.headers['Content-Type'] = 'multipart/form-data';
var response = await request.send();
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论