Malcolm
Malcolm

Reputation: 752

How to save data when the app is closed in Flutter

I'm having an app that has a bunch of user data that I save in a json file. I would like to make sure that when the user closes the app, the data is saved to the file for the next time the app is opened.

For now, the only solution I have is to save the file (and rewrite) everytime the user modifies something... so not the most efficient solution. Otherwise, I've only found solutions that involve using a SQL database (which I would like to avoid) or shared_preferences which is too simple for my data type (I believe).

My last idea was to use the background_fetch package since it has an option to trigger a function on close. Sadly that function seems to be outside of the app scope and so I cannot fetch the data in it.

Is there any solution to make sure the user won't loose data, while not saving the file continuously?

Upvotes: 1

Views: 6258

Answers (2)

Ozmen Celik
Ozmen Celik

Reputation: 169

I think the right and safety way is using pop modal when try to close or exit and let use chose save or not.

don't forget that u have to let user learn how to use app, not let them make u change app cause of misuse.

There is difference between the coding app to use simple interface and make it work safety and easy. Thats way u should think about using pop modal to let user chose if he wants to save change or not.

Also how about using sqfite as second db. U can use multi db in a project and its another way to save ur data in user phone.

Upvotes: 0

suztomo
suztomo

Reputation: 5202

Save the JSON document in

Upvotes: 3

Related Questions