Shared Preferences in Flutter

Shared Preferences in Flutter

What are Shared Preferences?

Android provides one of the most interesting and easy data storage options: Shared Preferences. It can be used to store and retrieve a small amount of primitive data as Key-Value pairs. Supported data types are int, double, bool, String and List<String>.

Where and how does the data gets stored?

It stores data by writing the key-value pairs to an XML file on the device's local storage inside the app. It can store data types such as String, Integer, Float, and Boolean. Imagine it like a map that store data as key-value pairs. The data will be lost if the user uninstalls the app from his/her device.

For example, let's take a scenario where you might want to store the user's login id and password if the remember me checkbox is checked while logging in.

When the user reopens the app after logging out the data will be prepopulated from the shared preferences data and the user just needs to click the login button and he'll enter the app.

Implement shared preferences in Flutter

Run this command:

With Flutter:

$ flutter pub add shared_preferences

This will add a line like this to your package's pubspec.yaml (and run an implicit flutter pub get):

dependencies:
shared_preferences: ^2.0.15

Import it

Now in your Dart code, you can use:

import 'package:shared_preferences/shared_preferenc..;

Write data to shared preferences

// Obtain shared preferences.
final prefs = await SharedPreferences.getInstance();

// Save an integer value to 'counter' key.
await prefs.setInt('counter', 10);

// Save an boolean value to 'isUserAdmin' key.
await prefs.setBool('isUserAdmin', true);

// Save an double value to 'user_rating' key.
await prefs.setDouble('user_rating', 1.5);

// Save an String value to 'username' key.
await prefs.setString('username', 'AdityaRathore');

// Save an list of strings to 'planets' key.
await prefs.setStringList('planets', ['Earth', 'Moon', 'Sun']);

Read data from shared preferences

// Try reading data from the 'counter' key. If it doesn't exist, returns null.
final int? counter = prefs.getInt('counter');

// Try reading data from the 'isUserAdmin' key. If it doesn't exist, returns null.
final bool? repeat = prefs.getBool('isUserAdmin');

// Try reading data from the 'user_rating' key. If it doesn't exist, returns null.
final double? decimal = prefs.getDouble('user_rating');

// Try reading data from the 'username' key. If it doesn't exist, returns null.
final String? action = prefs.getString('username');

// Try reading data from the 'planets' key. If it doesn't exist, returns null.
final List? items = prefs.getStringList('planets');

Remove data from shared preferences

// Remove data for the 'counter' key.
final success = await prefs.remove('counter');

Where and how?

Ok so now you know the syntax of using shared preferences in your application. You can use it according to your need by just following the below steps:

  1. Save data to shared preferences on button click or any call to action method.

  2. Retrieve the saved data on initState() method. So that the data can be used while building the widget tree and based on the data you can change the UI of the application.

Congrats 🥳 By now you know the basics of shared preferences and how to save, read and removed data from it.

Share This Tutorial

👉 Please share my posts with the community at daily.dev / social media by adding the article's URL to the feed.

By adding my article, I can share my insights and knowledge with other tech enthusiasts and contribute to the passionate community.

Cheers✨

In the next article, we'll learn how to implement shared preferences in the flutter application.