0

My flutter application allows users to pick up files from device storage and upload to server. Here is my code.

This is http request section

http.MultipartRequest request = new http.MultipartRequest("POST", uri);
request.headers.addAll(headers);

This is foreach loop section and output

files.forEach((element) async {
      request.files.add(await http.MultipartFile.fromPath(
        'files', element.path!,
        //contentType: new MediaType('application', 'octet-stream'),
      ));
    });
kLog('$serviceLog >>> start requesting....');
kLog('$serviceLog >>> File Length : ${request.files.length}');
http.StreamedResponse response = await request.send();

enter image description here

Foreach not getting the correct result although I use async and await keywords.

This is for loop section and output

for (var i = 0; i < files.length; i++) {
  request.files
      .add(await http.MultipartFile.fromPath("files", files[i].path!));
}
kLog('$serviceLog >>> start requesting....');
kLog('$serviceLog >>> File Length : ${request.files.length}');
http.StreamedResponse response = await request.send();

enter image description here

For loop is working without async keyword and the output correctly

Please enlighten me if there's something wrong with my codes

Triple K
  • 379
  • 1
  • 3
  • 14

0 Answers0