Home > Software engineering >  When to use Shared Preferences in flutter?
When to use Shared Preferences in flutter?

Time:11-19

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

CodePudding user response:

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.

CodePudding user response:

Store data in shared prefs if you want to make your data independ from app lifecycle ,your data will be saved even if app will be killed,or closed,even if device is restarted,otherwise if you are storing your data with some property static or not it will be cleared after app close. One more tip,there is a different between shared prefs and secure storage in case of shared pref your data will be loosed if user deletes your app,in case of secure storage your data will be exist if even after deleting and reinstalling.

  • Related