dartflutterdart-http

How to get progress event while uploading file on http.MultipartRequest request in flutter


I am uploading a file using MultipartRequest from package:http. I am successfully uploading the file but I want to get the progress of the file that is being uploaded. How can I achieve that? My current code looks something like this

Future submitFile(var report, File file) async {
var uri = Uri.parse(endpoint + "v1/reports");
  var request = http.MultipartRequest("POST", uri);
  await addHeaders(request.headers);
  request.fields.addAll(Report.toMap(report));
  if (file != null)
    request.files.add(await http.MultipartFile.fromPath(
      'report_resource',
      file.path,
    ));

  String response = "";
  await (await request.send()).stream.forEach((message) {
    response = response + String.fromCharCodes(message);
  });
  return response;
}

I searched for the solution, found this. And this post is somehow not similar to what I want to achieve, as he is using different client for the request.

Maybe I am not searching on the right path. Help is appreciated.


Solution

  • After waiting for a week or so. I didn't get response. Thus I developed a plugin myself to get this behavior. Package link.

    Example to use it:

    var request = MultipartRequest();
    
    request.addFile("image", imagePath);
    
    Response response = request.send();
    
    response.onError = () {
      print("Error");
    };
    
    response.onComplete = (response) {
      print(response);
    };
    
    //Not 100% success
    response.progress.listen((int progress) {
      print("progress from response object " + progress.toString());
    });
    

    Update Jun 30, 2020

    The package now supports iOS as well.