1

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.

Colly
  • 23
  • 7

3 Answers3

0

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?

kovalyovi
  • 855
  • 1
  • 8
  • 16
0

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.

Dharman
  • 26,923
  • 21
  • 73
  • 125
gordonturibamwe
  • 706
  • 1
  • 6
  • 13
0

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
    }
  }
MJ Montes
  • 2,968
  • 1
  • 15
  • 21