If you’re developing a mobile app in Flutter, you may want to store some data on the device, such as user preferences or app settings. Shared Preferences is a popular way to achieve this in Flutter. In this blog, we’ll explore what Shared Preferences are and how to use them in Flutter.

What are Shared Preferences?

Shared Preferences is a plugin for Flutter that allows you to store key-value pairs on the device. The data stored using Shared Preferences persists even if the app is closed, and can be accessed across different screens and sessions. Shared Preferences uses the native storage system of the device, which means that the data is stored on the user’s device in a location that is not accessible to other apps.

Why Shared Preferences?

If you want to save a small value or a flag that you want to get it later on the other parts of the applications or when the users launch the application. Shared Preferences is the best choice to perform with.

For storing small values SQLite is not the best option as you need to write lengthy codes and classes.

Shared Preferences allows you to read and write with key-value pair in couples of code lines easily. But Shared Preferences is not a best choice to keep complex relational data.

For example, Shared Preferences can be used to store the logged-in status of the user so that the user does not have to log in or log out again and again. Another example of Shared Preferences can be used to store the theme of the applications i.e Dark Mode or Light Mode.

How to Use Shared Preferences in Flutter

To use Shared Preferences in your Flutter app, you need to follow these steps:

Step 1 Adding Shared Preference packages

Add the package to the pubsec.yaml file

dependencies:
  shared_preferences: ^2.0.10

Step 2: Import the Shared Preferences package

Once you have added the Shared Preferences dependency to your pubspec.yaml file, you need to import the package in your Dart code. To do this, add the following code to the top of your file:

import 'package:shared_preferences/shared_preferences.dart';

Step 3: Store data using Shared Preferences

Now that you have imported the Shared Preferences package, you can use it to store data in your app. To store data, you first need to create an instance of SharedPreferences. You can do this using the following code:

SharedPreferences prefs = await SharedPreferences.getInstance();

Once you have an instance of SharedPreferences, you can use it to store data. For example, to store a boolean value, you can use the setBool() method. SharedPreferences can store almost all types of available data types.

You can store the value as follows:

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

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

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

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

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

// Save an list of strings to 'items' key. 
await prefs.setStringList('items', <String>['Sun', 'Mond', 'Tue']);

Step 4: Retrieve/ Read data using Shared Preferences

To retrieve data that you have stored using Shared Preferences, you can use the get() method. For example, we can use the following code:

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

// get an integer value from 'num' key. 
await prefs.getInt('num');

// get an boolean value from 'flag' key. 
await prefs.getBool('flag');

// get an double value from 'decnum' key. 
await prefs.getDouble('decnum');

// get an String value from 'name' key. 
await prefs.getString('name');

// get an list of strings from 'items' key. 
await prefs.getStringList('items');

Step 5: Remove data from Shared Preferences

To remove data that you have stored using Shared Preferences, you can use the remove() method and key.

await prefs.remove('key');

For example, if you need to remove the value of key items

await prefs.remove('items');

Conclusion

Shared Preferences is a useful way to store data on the device in a Flutter app. It is easy to use and allows you to persist data even if the app is closed. By following the steps outlined in this blog, you can start using Shared Preferences in your Flutter app today.

Thanks for reading this article 

Also, follow to get updated on exciting articles and projects.

If I got something wrong? Let me know in the comments. I would love to improve.

Let’s get connected

We can be friends. Find on FacebookLinkedinGithubYouTube

BuyMeACoffee, and Instagram.

Contribute: BuyMeACoffee

ContactContact Us