Flutter How to know Workmanager has finished

427 Views Asked by At

I use Flutter with the plugin Workmanager. I did not find a way to know if the background task has finished for registerOneOffTask. I had ideas with RxSharedPreferences that I write something from the Workmanager into SharedPreferences and then observe it in my main application but this seems to be quite dirty.

Is there an elegant way to know if the Workmanager task is finished?

1

There are 1 best solutions below

0
On

Before you're executeTask is about to finish, you can write the result to SharedPreferences using the task's unique name you will provide.

While the tasks are running, run a 1 second timeout to check the running tasks' result change by observing their SharedPreferences keys. Once a value of a key changes, remove the entry from SharedPreferences , and handle the result as needed.