Flutter Shared Preferences in depth

What is shared_preferences where, when and how much to use it?

The shared_preference is a plugin that allows you to save data app dedicated memory, the folder that will contain this data is named in Android with the package name of your app, e.g. com.companyname.appname.

The data that we can store in this space with this plugin are data of these types:

  • String

  • Integer

  • Bool

  • List

It should be noted that these parameters are not persistent, they remain in the area dedicated to the app therefore are eliminated when the app is uninstalled or if the app cache data is removed. So, in general it is right to store non-essential preference data so that it is not a tragedy if they have been lost. A classic example can be the app theme color light or dark or the order of menu title.

How to use?

Saving:

import 'package:shared_preferences/shared_preferences.dart';
SharedPreferences _preferences = await SharedPreferences.getInstance();
_preferences.setString("theme", "dark");
_preferences.setInt("countaccess", 2);

Recovery:

import 'package:shared_preferences/shared_preferences.dart';
SharedPreferences _preferences = await SharedPreferences.getInstance();
_preferences.getString("theme");
_preferences.getInt("countaccess");

At first we might think that it is of little use since in a string we cannot store a lot of information. In fact, if we think about it in a string, we can also store a lot but really a lot by using a json, below an example:

import 'package:shared_preferences/shared_preferences.dart';
SharedPreferences _preferences = await SharedPreferences.getInstance();
_preferences.setString("{"name":"Andrea","surname":"Luciano","age":31,"sex":"M","SurnameCouldBeName":true}");

In this example I put SurnameCouldBeName as a field because my last name is also a first name. All those who don’t know me always ask me “Luciano is your name or surname?”

And every time I would like to do something like that:

We can therefore create a collection of people using a list of strings instead of the single string as follows:

import 'package:shared_preferences/shared_preferences.dart';
SharedPreferences _preferences = await SharedPreferences.getInstance();
List<String> users = new List<String>();
users.add("{"name":"Andrea","surname":"Luciano","age":31,"sex":"M","SurnameCouldBeName":true}");
users.add("{"name":"elisa","surname":"luciano","age":32,"sex":"F","SurnameCouldBeName":false}");
_preferences.setStringList("users", users);

This could finally be mapped into an object in order to access the data simply and finally show it to the interface:

Class UserDto()
{
String name, surname, age, sex, SurnameCouldBeName;
UserDto.fromJson(Map<String, dynamic> json)
: name= json[name],
surname= json[surname],
age= json[age],
sex= json[sex],
SurnameCouldBeName = json[SurnameCouldBeName];
}
SharedPreferences _preferences = await SharedPreferences.getInstance();
List<UserDto> UserObj = new List<UserDto>; 
List<String> users = _preferences.getStringList("users");
for(String userjson in users)
{
Map userMap = jsonDecode(userjson);
UserDto  = new UserDto.fromJson(userMap);
print(UserDto.name);
UserObj.add(UserDto);
}

Think about the possible benefits and applications of this. It can be used to store data returned by an API. In this way, the application would be more efficient and also can work offline without therefore having to use network data for recovering data from the server.

Last modified: 15 May 2020