Shared Preferences

·

3 min read

Shared Preferences is a simple key-value storage system provided by the Flutter framework. This storage system is used to store small amounts of data that are required by the application across different screens. Shared Preference is an ideal solution to save data like user preferences, app configurations, or any other small data that the app needs to remember. In this blog, we will explore Shared Preferences and how to use them in Flutter applications.

Why use Shared Preferences?

Shared Preferences is an essential storage system in Flutter because it allows developers to store and retrieve small amounts of data quickly and efficiently. This system is particularly useful when you want to store user preferences or other small amounts of data that the application requires.

Some of the benefits of using Shared Preferences are:

  • Ease of use: Shared Preferences is easy to use, and you can store and retrieve data with just a few lines of code.

  • Fast and efficient: Shared Preferences is fast and efficient, and they can quickly store and retrieve small amounts of data.

  • Data persistence: Shared Preferences are persistent, which means that data stored using this storage system is not lost when the application is closed.

Implementing Shared Preferences in Flutter

Now that we know what Shared Preferences are and why it is essential let's look at how to implement them in Flutter.

First, we need to add the Shared Preferences package to our project. We can do this by adding the following dependency to our pubspec.yaml file:

dependencies:
  flutter:
    sdk: flutter
  shared_preferences: ^2.0.8

Once we have added the dependency, we can import the package into our Dart file using the following statement:

import 'package:shared_preferences/shared_preferences.dart';

Now that we have imported the package let's look at how to store and retrieve data using Shared Preferences.

Storing Data

To store data using Shared Preferences, we need to create an instance of the SharedPreferences class. We can do this using the getInstance() method. The getInstance() the method returns a future that resolves to an instance of the SharedPreferences class.

Here's an example of how to create an instance of the SharedPreferences class:

SharedPreferences prefs = await SharedPreferences.getInstance();

Once we have an instance of the SharedPreferences class, we can use it to store data. We can store data using the setString(), setBool(), setInt(), and setDouble() methods. Each of these methods takes two arguments: a key and a value.

Here's an example of how to store a String value using Shared Preferences:

prefs.setString('name', 'John');

In this example, we are storing a String value 'John' with the key 'name'. We can store other data types using their respective set methods.

Retrieving Data

To retrieve data using Shared Preferences, we need to create an instance of the SharedPreferences class and use the get methods to retrieve the data. We can retrieve data using the getString(), getBool(), getInt(), and getDouble() methods. Each of these methods takes a single argument: the key of the value that we want to retrieve.

Here's an example of how to retrieve a String value using Shared Preferences:

String name = prefs.getString('name');

In this example, we are retrieving the String value stored with the key 'name' and assigning it to the name variable.

Removing Data

We can also remove data stored using the Shared Preferences using remove() the method. The remove() method takes a single argument: the key of the value that we want to remove.

Here's an example of how to remove a value using Shared Preferences:

prefs.remove('name');