Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to use http.post in compute() function

Tags:

flutter

I'm trying to use the compute() function to trigger a top level method that calls an HTTP POST.

I can see the method being executed, but then it just hangs where I do the actual post, with no errors returned.

However if I call this without calling compute, it works fine

uploadData(Map args) {
  print("uploading data"); // i see this in the logs

  API().uploadData(args["data"], args["user"], args["apikey"]);
}

and it's called via

 compute(uploadData, {
        "data": dataList,
        "user": widget.userProps,
        "apikey": widget.apiKey
      });

lastly this is my API uploadData method

  uploadData(List files, User userdata, String apikey) async {
    try {
      String sessionid = await _getSession();
      String _base = 'http://192.168.2.13:3000/upload';
      String body = json.encode({
        "api": apikey,
        "user": userdata.toMap(),
        "data": files,
        "sessionid": sessionid
      });
      print("I AM HERE"); // this is called
      await http.post(_base,
          body: body, headers: {"Content-Type": "application/json"});
     print("this is not called");
    } catch (e) {
      print("Error"); // no error
      print(e);
    }
  }
like image 608
Faisal Abid Avatar asked Sep 16 '25 00:09

Faisal Abid


1 Answers

you have to return, because API().uploadData returns a future (of null, I assume). If we don't return, API().uploadData is executed and then uploadData(Map args) return null value and pops out of the stack immediate; isolate has no functions, microtask queue or event queue left, it will exit.

uploadData(Map args) {
  print("uploading data"); // i see this in the logs

  return API().uploadData(args["data"], args["user"], args["apikey"]);
}
like image 71
TruongSinh Avatar answered Sep 19 '25 15:09

TruongSinh