I'm currently making a timer page that lists your times when you start and stop, but when i close the page or restart the app all the times disappear. I want to know how i can save the state of the page so it just returns back with all the times on the page. Here is my project.
4 Answers
The state exists while the page (route) is in memory, and once you remove the page from memory, the state loses its data. It's like the computer RAM - the application is stored in the RAM and once it's not used, that memory is freed up. In order to persist data, I would suggest using local storage (saving to the device and loading from it). Here is the link to the following article: How to save to local storage using Flutter?

- 1,006
- 1
- 9
- 19
You should check out Flutter Preferences for local storage to help you save timeStamps And also use AppLifecycleState to listen when AppLifecycleState.paused, AppLifecycleState.inactive or AppLifecycleState.resumed
When app is paused or inactive, save timer in preferences, when it has resumed, continue from the last time saved plus the time difference since last time app was inactive or paused.

- 30,962
- 25
- 85
- 135

- 843
- 1
- 7
- 15
You can use didChangeAppLifecycleState
with SharedPreferences or you can also use hydratedbloc package and it will handle the persisting of state for you automatically.
didChangeAppLifecycleState(AppLifecycleState state) {
if(state == AppLifecycleState.detached) {
//save to SharedPreference
}
}

- 3,234
- 1
- 19
- 21
You could look into Restoration Mixins. See here: https://api.flutter.dev/flutter/widgets/RestorationMixin-mixin.html
Copy pasting from the above link:
Restoration data can be serialized out and, at a later point in time, be used to restore the stateful members in the State object to the same values they had when the data was generated.
This mixin organizes the restoration data of a State object in RestorableProperty. All the information that the State object wants to get restored during state restoration need to be saved in a subclass of RestorableProperty. For example, to restore the count value in a counter app, that value should be stored in a member variable of type RestorableInt instead of a plain member variable of type int.
The mixin ensures that the current values of the RestorablePropertys are serialized as part of the restoration state. It is up to the State to ensure that the data stored in the properties is always up to date. When the widget is restored from previously generated restoration data, the values of the RestorablePropertys are automatically restored to the values that had when the restoration data was serialized out.
Also see https://api.flutter.dev/flutter/services/RestorationManager-class.html
Copy pasting from above link:
Restoration data can be serialized out and at a later point in time be used to restore the application to the previous state described by the serialized data. Mobile operating systems use the concept of state restoration to provide the illusion that apps continue to run in the background forever: after an app has been backgrounded, the user can always return to it and find it in the same state. In practice, the operating system may, however, terminate the app to free resources for other apps running in the foreground. Before that happens, the app gets a chance to serialize out its restoration data. When the user navigates back to the backgrounded app, it is restarted and the serialized restoration data is provided to it again. Ideally, the app will use that data to restore itself to the same state it was in when the user backgrounded the app.

- 1
- 1