I'm trying to upload multiple images in backend.
I got this response when I'm trying to print files:
[Instance of 'MultipartFile', Instance of 'MultipartFile', Instance of 'MultipartFile']
but at a server side I got null array {}
. This is my method. I'm using http
for api communication.
Future<Map<String, dynamic>> postWithMultiImage(
String _url,
Map<String, String> _headers,
Map<String, String> _params,
String _imageKey,
List _imageFile) async {
if (_headers != null) {
print('_headers => $_headers');
}
print('_params => $_params');
print('_url => $_url');
var request = http.MultipartRequest("POST", Uri.parse(BASE_URL + _url));
if (_headers != null) {
request.headers.addAll(_headers);
}
if (_params != null) {
request.fields.addAll(_params);
}
if (_imageFile != null) {
for (int i = 0; i < _imageFile.length; i++) {
final _type = lookupMimeType(_imageFile[i]);
final _name =
'${DateTime.now().toIso8601String()}.${_type.split('/').last}';
final _partFile = http.MultipartFile(
_imageKey,
File(_imageFile[i]).openRead(),
File(_imageFile[i]).lengthSync(),
filename: _name);
request.files.add(_partFile);
}
print('request files: ${request.files}');
}
var response = await request.send();
final code = response.statusCode;
print('response code => $code');
final responseBody = await http.Response.fromStream(response);
final body = responseBody.body;
final jsonBody = json.decode(body);
Map<String, dynamic> _resDic;
if (code == 200) {
_resDic = Map<String, dynamic>.from(jsonBody);
_resDic[STATUS] = _resDic[SUCCESS] == 1;
} else {
_resDic = Map<String, dynamic>();
_resDic[STATUS] = false;
_resDic[IS_TOKEN_EXPIRED] = 0;
_resDic[MESSAGE] = jsonBody[MESSAGE] != null
? jsonBody[MESSAGE]
: 'Something went wrong';
}
_resDic[HTTP_CODE] = code;
return _resDic;
}
Thanks in advance.