is flutter (dart) able to make an api request in separate isolate?
Asked Answered
S

3

10

I made a function to post notification to a topic. It works great in normally, then I put it in compute function and hope it can posts notification in the background. But it not works. Here is my code:

void onSendMessageInBackGround(String message) {
  Future.delayed(Duration(milliseconds: 3000)).then((_) async{
    Client client = Client();
    final requestHeader = {'Authorization': 'key=my_server_key', 'Content-Type': 'application/json'};
    var data = json.encode({
      'notification': {
        'body': 'tester',
        'title': '$message',
      },
      'priority': 'high',
      'data': {
        'click_action': 'FLUTTER_NOTIFICATION_CLICK',
        'dataMessage': 'test',
        'time': "${DateTime.now()}",
      },
      'to': '/topics/uat'
    });
    await client.post('https://fcm.googleapis.com/fcm/send', headers: requestHeader, body: data);
  });
}

call compute:

compute(onSendMessageInBackGround, 'abc');

Note: I has put the onSendMessageInBackGround function at the top level of my app as the library said

Is it missing something? or we can't do that?

Seiter answered 16/11, 2018 at 7:41 Comment(8)
It should work. There are currently limitations using plugins in isolates. I don't see a good reason to run your code in an isolate because it's unlikely to cause much load on the UI isolate anyway.Aquatic
indeed: your code does not contain any CPU blocking parts, so there is really no need to use compute hereMolli
You didn't any information about the current behavior. Do you get any error message?Aquatic
it just a sample I write to check the sperate isolate can work with the server or not. I hope it can replace a part of Android Service in some case and work as a service in IOSOujda
@GünterZöchbauer no it not throw any error :((Oujda
Could mean that it's working fine ;-)Aquatic
@GünterZöchbauer when I do not put it in compute and run it like a normal function like that onSendMessageInBackGround('abc'). My message is posted to firebase and the other devices can receive notification :))Oujda
What's the point of Future.delayed(Duration(mi?Aquatic
D
2

You might need to add a return or await

void onSendMessageInBackGround(String message) {
  return /* await (with async above) */ Future.delayed(Duration(milliseconds: 3000)).then((_) async{

It could be that the isolate shuts down before the request is made because you're not awaiting the Future

Dibrin answered 16/11, 2018 at 8:17 Comment(1)
Thank you bro, this and other answers of yours helped me a lot with isolates/compute!Pahari
M
0

Function called from compute must be static or global.

Either i agree with pskink, compute here is not usefull.

Martine answered 16/11, 2018 at 8:3 Comment(0)
W
0

Isolates communicate by passing messages back and forth. These messages can be primitive values, such as null, num, bool, double, or String, or simple objects such as the List in this example.

You might experience errors if you try to pass more complex objects, such as a Future or http.Response between isolates.

Got this from the documentation here

Webworm answered 11/5, 2020 at 7:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.