How to send data to the main isolate from workmananger's isolate?

300

After some research the solution is as follows:

First you register a port on the initialization of the main isolate like this:

void listenToUpdatesFromWorkManager(String name) { var port = ReceivePort(); IsolateNameServer.registerPortWithName(port.sendPort, name); }

You give it a unique name in order to identify it from any other opened isolates like this:

SendPort sendPort = IsolateNameServer.lookupPortByName(name);

Share:
300
anass naoushi
Author by

anass naoushi

Updated on December 01, 2022

Comments

  • anass naoushi
    anass naoushi over 1 year

    My problem is that flutter Hive cannot be opened from multiple isolates. So I want the data fetched in workmananger task to be sent to the main isolate where the hive box is opened and modify it there to avoid corrupting it. I should take into consideration when the app is alive and when it is not => for example when the app is not alive, I edit the hive file directly since it will be only opened in the workmanager isolate whereas if the app is not alive I send the data to the main isolate and edit the hive file. My problem is that I do not know how to track lifecycle within the workmanager task and I do not know how to send the data to the main isolate. Any workarounds or solutions for the problem or how to code the above? Thank you in advance.