Sam
Sam

Reputation: 2093

flutter: when to save to Shared Preferences for best performance?

I've been reading the tutorials etc. and I'm now implementing Shared Preferences to save variables between screens. In my app the user moves to and from items in a ListView widget, and may return to a particular screen, I need to save their activity there in case of that.

In Java on Android you would save to sharePrefs when the user exits an activity (screen) because it's a slow operation and saving each time a variable changes is not performant.

Is there a best practice time to save in flutter? How do you know a user is exiting a screen, and descending back to an earlier dart file?

Apologies if this is a turbo ignorant question.

Upvotes: 3

Views: 3769

Answers (1)

GrahamD
GrahamD

Reputation: 3175

When you exit a page in Flutter, the dispose method is called so I think you could save the Shared Preferences in that method. Here's a pretty good article on this area: https://medium.com/flutter-community/widget-state-buildcontext-inheritedwidget-898d671b7956

Upvotes: 1

Related Questions