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.
2
Answers
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.
To post image you should use a formdata.