Shared Preferences in Flutter: Store and Retrieve data locally
Store and retrieve data locallyFlutter is a popular framework for building cross-platform mobile applications. One of the essential features in mobile development is the ability to store data persistently across app sessions. Shared preferences in Flutter are one way to store and retrieve data in a key-value pair format. In this blog, we will explore what shared preferences are and how to use them in Flutter with examples.
What are Shared Preferences in Flutter?
Shared preferences are key-value pairs used to store and retrieve data in a Flutter application. They are often used to store simple user preferences, such as language settings, font size, or theme colors. Shared preferences are stored in the device’s file system and are persistent across app sessions.
Data once stored in shared preferences remain in the device's local storage, hence data can be retrieved whenever the app is launched. But Shared Preferences data is lost whenever the App Data is deleted or the App is uninstalled from the device.
Shared preferences are commonly used in Flutter applications because they are easy to use and provide a simple way to store and retrieve data. They are also lightweight and can store small amounts of data. Shared preferences data is stored in XML Format.
How to use Shared Preferences in Flutter
Shared preferences should be used only to store simple data like String, int, bool, etc, and NOT complex data like a dictionary / JSON / List of objects received from API.
To use Shared Preferences
in Flutter, you need to add the following dependency to your pubspec.yaml
file:
dependencies:
shared_preferences: ^2.0.12
Then, run the command flutter packages get
in your terminal to install the package.
Once you have added the shared_preferences
package, you can use it in your Flutter application.
To store data in Shared Preferences
, you need to follow these steps:
- Get an instance of
SharedPreferences
- Store data in
SharedPreferences
- Retrieve data from
SharedPreferences
Let’s go through each step with examples.
Step 1: Get an instance of SharedPreferences
To get an instance of SharedPreferences
, you can use the getInstance()
method from the SharedPreferences
class. Here's an example:
import 'package:shared_preferences/shared_preferences.dart';
SharedPreferences preferences = await SharedPreferences.getInstance();
The await
keyword is used to wait for the getInstance()
method to complete before returning the SharedPreferences
instance.
Step 2: Store data in SharedPreferences
To store data in SharedPreferences
, you can use the set<Type>(key, value)
method, where Type
can be String
, int
, double
, bool
, or List<String>
. Here's an example:
SharedPreferences preferences = await getSharedPreferencesInstance();
preferences.setString('username', 'johndoe');
preferences.setInt('age', 30);
preferences.setBool('isLogged', true);
In the example above, we store a String
, an int
, and a bool
in SharedPreferences
using the setString, setInt and setBool
methods.
Step 3: Retrieve data from SharedPreferences
To retrieve data from SharedPreferences
, you can use the get<Type>(key)
method, where Type
is the type of value you want to retrieve. Here's an example:
SharedPreferences preferences = await getSharedPreferencesInstance();
String username = preferences.getString('username');
int age = preferences.getInt('age');
bool isLogged = preferences.getBool('isLogged');
In the example above, we retrieve the String
, int
, and bool
values we stored earlier using the getString, getInt and getBool
methods.
Step 4: Delete data from SharedPreferences
To retrieve data from SharedPreferences
, you can use the remove(key)
method, where key
is the name of the data you want to remove. Here's an example:
SharedPreferences preferences = await getSharedPreferencesInstance();
preferences.remove('username');
preferences.remove('age');
preferences.remove('isLogged');
Step 5: To Check whether a value is present or not?
containsKey
used to check value is present or not.
SharedPreferences preferences = await getSharedPreferencesInstance();
bool checkValue = preferences.containsKey('age');
containsKey
will give a boolean value, true for present and false for not present.
Conclusion
In this blog, we discussed how to use Shared Preferences
in Flutter with examples. We learned how to get an instance of SharedPreferences
, store retrieve and check if data exists in SharedPreferences
. Shared Preferences
is a simple and easy-to-use way to store and retrieve simple data in Flutter.
❤ ❤ Thanks for reading this article ❤❤
If you find this blog informative do give a clap 👏 below.
Let's connect on LinkedIn.
Read my other blogs here: