Shared Preferences Package In Flutter : How To Implement It

Supported Platforms: Android, iOS, Linux, macOS, Web, Windows
Popularity: 8713 Likes 

To store simple variables such as bool, int, and string in your application. You can use shared preferences instead of having a database.

What is SharedPreferences?

SharedPreferences is only used to store the user’s small amount of data within the application, such as settings and data (not too much; if too much, then a database is required).
SharedPreferences in Flutter are kept in XML format. It is also compatible with iOS and Android.
Assume you want to store a small value (likely a flag) that you want to be able to access later when a user opens the application. Shared preference then takes effect.
For small-value storage, we do not use SQLite since doing so requires writing complex code and supporting classes.


* The purpose of SharedPreference is to store user data within the app.
* Deleting application data or uninstalling an app, shared preference data will be lost.
* SharedPreference should not be used as a database.
With SharedPreference, key-value pairs can be easily read and written in a few lines. Remember, though, that SharedPreferences is not a way for you to store complex relational data.

How to use SharedPreferences in Flutter?

You should be aware that the Flutter SDK does not support SharedPreferences before using them, but thankfully, you can use the shared_preferences plugin to store key-value data on the disc.


Step 1: Adding the dependencies

We need to install the required package before we can start using SharedPreferences. Using pubspec.yaml in our dart project, we will open it for this purpose and add the following line under the “dependencies” section:

   sdk: flutter
 shared_preferences: "<newest version>"

Following the modifications, there are two ways we can install the package:

To obtain packages, right-click on the pubspec.yaml file and select “get packages.”

Insert the subsequent command into the terminal:

flutter pub get

The shared_preferences package will be automatically updated when we execute the following command, saving us the trouble of going through each of the previously mentioned steps.

flutter pub add shared_preferences

Step 2: Import shared_preferences.dart

import 'package:shared_preferences/shared_preferences.dart';

Step 3: Save data

With SharedPreferences, we can only add int, String, double, and bool.

The SharedPreferences class has setter methods that accept the parameter’s key and value.

Saving String value

addStringToSF() async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  prefs.setString('stringValue', "abc");

Saving int value

addIntToSF() async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  prefs.setInt('intValue', 123);

Saving double value

addDoubleToSF() async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  prefs.setDouble('doubleValue', 115.0);

Saving boolean value

addBoolToSF() async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  prefs.setBool('boolValue', true);

Step 4: Read data

We only need to pass the key when using SharedPreferences to read data from the storage.

Read string value

getStringValuesSF() async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  //Return String
  String stringValue = prefs.getString('stringValue');
  return stringValue;

Read boolean value

getBoolValuesSF() async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  //Return bool
  bool boolValue = prefs.getBool('boolValue');
  return boolValue;

Read int value

getIntValuesSF() async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  //Return int
  int intValue = prefs.getInt('intValue');
  return intValue;

Read double value

getDoubleValuesSF() async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  //Return double
  double doubleValue = prefs.getDouble('doubleValue');
  return doubleValue;

We might receive a null value if the value is not present in the storage.

We can use it to deal with this.

int intValue= await prefs.getInt('intValue') ?? 0;

Step 5: Remove data

We can add the key to the .remove(String key) method to remove the data from the storage.

removeValues() async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  //Remove String
  //Remove bool
  //Remove int
  //Remove double

Check value if present or not:

SharedPreferences prefs = await SharedPreferences.getInstances.getInstance();
Bool checkValue = prefs.containsKey('value');

If the given key is present in persistent storage, containsKey will return true; otherwise, it will return false.

We can efficiently use your Flutter app’s Shared Preferences to store and retrieve small amounts of data over time by following these steps. It’s critical for controlling user preferences and settings, improving the user experience as a whole, and designing an intuitive user interface for your application.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More