When to use Shared Preferences in flutter?


Asked by isso k on November 18, 2021 (source).

I'm just curious about when is the perfect time to use shared preferences, I know that it is perfect to use it when we have to save login data for example. so, for example I'm working on a project,

the user must choose a service for example electricity, after that the app will ask him if this order is urgent and of course I have to save that he chose electricity then the app after that will ask him to write his issue, after that the app will show him a list of electricians so it is a must for the app to be saving the "electricity" that he chose it first.

the question is, does i have to save it in shared preferences or I just have to send the data through screens like navigate to secondScreen("electricity")

can any one just inform me with more information about flutter to be more professional in my code


Question answered by developerjamiu (source).

You should store in Shared Preferences when you want your data to be available on app restart otherwise just store in memory.

For example:

To work with theme (dark or light theme). You want to app to behave in such a way that when you change the app theme and you restart the app, the app should maintain the theme to changed to earlier. In this case (and similar cases), it's better to store in Shared Preferences (Local Storage).

However, if the data you're storing is not useful after app restart (like in your case), you don't need to store the data in Shared Preferences.