Sharedpreferences is a plugin that stores key-value pairs in your device. You can use it on mobile devices, Android, iOS, the web and desktop devices (Windows, Linux and Mac).
This is possible because “shared_preferences” envelops different technologies depending on the operating system where your application is installed. For instance, on iOS, it’s based on the NSUserDefaults class. On Android it leverages the SharedPreferences interface, and on the web, it uses the window.localStorage object. This means you have a universal way to save your data locally, without any code duplication, regardless of the target system.
What’s more, as you’ll see shortly, SharedPreferences is also very easy to use, making it an excellent choice for storing simple data. By ‘simple data’, we’re referring to strings, numbers(like int or double), booleans, and string lists. Of course you can also save Dart objects as strings.
Generally speaking, sharedpreferences should not be used for critical data, or large amounts of data: in these cases, you’d better choose a SQL or NoSQL database.
It’s important to note that SharedPreferences data is stored within the application itself. So, if a user decides to uninstall your app, all the data in SharedPreferences will be deleted. This is in fact one of the issues with local storage in general. So if your data is important always make sure to backup your data, in the cloud or in any case in a different device.
So, let’s have a look at the high-level steps involved in using SharedPreferences.
First you need to get an instance of SharedPreferences. You do that by invoking the getInstance method. Note that this is asynchronous. So you’ll have to use await / async method, or the then callback, to ensure that the instance has been successfully returned by the method.
In the slide on the screen, you see an example of how to get an instance of SharedPreferences:
prefs = await SharedPreferences.getInstance();
Once you get the instance, you can read and write data with the Sharedpreferences methods, that depend on the type of data you want to deal with. for example,if you want to store a string, you can use the setString method:
await prefs.setString('myStringKey', 'Flutter is great!');
In the example you see on the slide, the setString method takes two parameters, a key and a value: myStringKey is the key, and ‘Flutter is great” is the value. Note that all write actions are asynchronous.
If you want to save an integer number the instruction is very similar: you can use the setInt method, again with a key and a value:
await prefs.setInt('myIntKey', 42);
In this case myIntKey is the key, and 42 is the value you want to store. As you might expect, setBool and setDouble store booleans and doubles , and setStringList handles lists of strings.
Now, how dow you retrieving these values?
That’s where getters come in - getInt retrieves an integer, while getString reads a string value:
int myValue = prefs.getInt('myIntKey'); //42
String myStringValue = prefs.getString('myStringKey'); //'Flutter is great!'
Note that unlike setters, getters are not asynchronous - they return the values directly, so there’s no need to await anything here.
If you want to delete a value, you can use the remove method, passing the key:
bool isDeleted = await _sp.remove('myKey');
As this is writing operation, it is asynchronous.
Good, it’s time dive into some coding! let’s have a look at the first demo of this course!