Home > Net >  Save full state of a Flutter Application
Save full state of a Flutter Application

Time:11-03

Is there a way to save then restore the full state of a Flutter application ?

By full, I mean saving the states of all widgets and objects

For example, a TextField will have the exact text on it, the exact color, highlights,

A GoogleMap will have the exact Zoom states, Markers, Polylines

Every objects internal states will be kept, even private fields.

...

I can't do it manually for three main reasons:

  1. There are too many states, any wrong state can lead to a butterfly effect of discrepancy
  2. Some states of some classes are impossible to get and set as they are internal (private)
  3. Some states are random at instantiation

I figured out the technical term is https://en.wikipedia.org/wiki/Continuation

There is an important use case for that:

User could Save current state of their work then resume later. (More like "Save" in games)

CodePudding user response:

No, there is no automatism to save "all", because "all" would be impractical and impossible to determine. After all, you don't have endless time or disk space before a mobile operating systems kicks your app out of it's memory, you should not just "dump all" because it might be too much for any real app.

Some good info can be found here:

Flutter app restoration; how to save the app state when the activity is killed by the system?

If you are using the BloC state management approach, it has a package called hydrated_bloc, that basically manages your BloCs storage so it can be restored to it's previous values when you open your app the next time.

Flutter itself seems to have a RestorationManager class to handle this, but it has no automatisms, you need to do that. So all controls or packages you use may not save their state and then you are out of luck and back to the way where you determine what is important and what can be lost.

CodePudding user response:

There are several state-managing packages out there . I don’t think there is a way to do that. Flutter gives the freedom of managing states with whatever suits you. You don’t need to save textfield colors, or other properties that aren’t to be interacted by the user. You only need to manage states of widgets that the user interacts with (eg, Textfield value).

I currently use sqflite to store data in my application and provider package to manage state of my app

So my advice is:

  • Figure out what exactly needs to be stored and kept for the user
  • Decide what you want to use to store data (eg: local database, sharedPreference, etc)
  • Related