I am wondering if its reliable to store some of the user data such as name, age, weight, Dark/Light mode ect with shared prefrences.
Note that I already have stored these values in Firebase but I dont want to keep retrieving from Firebase everytime I want to show these data on the app so I was thinkin of shared prefrences as a solusion for faster load and less usage of resources. The other solution that I was thinking of is to use sqlite but I think its overcomplicated to just store this simple values.
What are your thoughts on this?
CodePudding user response:
It depends on what you mean by reliable.
But in general, it is reliable. I have my whole Flutter app stored offline in shared preference and it is quite advantageous because it reduces the number of network calls. API calls can fail, making them less reliable. They are also time-consuming to implement.
Shared preference is quite easy to implement. The disadvantage I found is that you have to serialize and deserialize your data as it does not support a complex data model.
This causes an issue when your app evolves. For example, your User class may today have age and eight and name, but in the future, you may want to add gender, address, and title. This will be problematic to handle as you need to write extra code to handle the old version, otherwise, your app will crash.
Also, another disadvantage is that the user can clear the data of your app from settings. This will delete all shared preferences. But in that case, I handle it with an API call to rewrite the data.
SQLite is quite an overkill for what you are trying to achieve. As a rule of thumb, if your data is not growing too big like a list of items, in your case you have only a single document (the User document), prefer to use shared preference. They are fast to retrieve and easy to program.
CodePudding user response:
Ya, it's reliable to save user data in any kind of local storage.
Encode and Save in Shared Pref:-
final prefs = await SharedPreferences.getInstance();
await prefs.setString('user', jsonEncode(User.fromJson(data)));
Get value:
final String? user = jsonDecode(await prefs.getString('action'));
Make sure to be upto date with the data.
CodePudding user response:
It is totally reliable but while saving personal information like name, email, phone, etc. Please shote the information in an encrypted format. So that personal information will be safe.