dingo
dingo

Reputation: 906

Flutter: Executing a Task after App is Killed

I would like to execute some asynchronous code just before my Flutter App is killed by the OS due to low memory.

I am currently using the WidgetsBindingObserver mixin to listen to app lifecycle changes, and when the app is paused (before it is detached) start the code execution.

The code I'm trying to execute: I am trying to commit something to a local cache using SharedPreferences, but to no avail. It doesn't end up committing the data.

The Potential Issues: I'm not sure if it is primarily due to running code while the app is being detached - in which case it could be solved by using the WorkManager package. Or, if it is related to accessing Shared Preferences after the app has already been detached.

Any help or insight would be welcome!

Upvotes: 3

Views: 2327

Answers (1)

dingo
dingo

Reputation: 906

Unfortunately, using the WidgetsBindingObserver mixin doesn't work for my use-case. When the lifecycle status changes to detached, any asynchronous code executed after this is not able to complete in time.

I solved the problem using a local key-pair store called Hive to locally save the form data as it was inputted by the user. Then, when they are ready to resume the form completion, re-populate the form and allow them to continue filling it out.

Upvotes: 2

Related Questions