Working with Shared Preferences in Flutter
Working with Shared Preferences in Flutter
Shared preferences is a persistent storage mechanism in Android that allows you to store and retrieve small pieces of data. In Flutter, we can implement shared preferences for both Android and iOS. It helps to develop apps faster as you don’t need to write your own solution or use third-party libraries.
Using shared preferences helps store user information such as login data or favorite items in the app. The data stored in the shared preferences is immediately available everywhere in the app. This makes them a great choice for storing user preferences, allowing the user to tailor the app to their own personal preferences.
In this tutorial, we will learn how to use shared preferences in Flutter and start interacting with some of its most popular methods.
Getting Started with Shared Preferences in Flutter
The first step you need to do is import the package into your project. You can do this by adding the following line of code to your pubspec.yaml file.
dependencies:
shared_preferences: ^version-number
Once you have imported the package into your project, you will need to initialize it. To do this, you will need to add the following code to your main.dart file:
import 'package:shared_preferences/shared_preferences.dart';
Future main() async {
WidgetsFlutterBinding.ensureInitialized();
SharedPreferences prefs = await SharedPreferences.getInstance();
runApp(MaterialApp(
home: YourApp(),
));
}
This code will create an instance of the SharedPreferences object which you will use for the rest of your tasks. Once you have initialized the sharePreferences object, you are ready to start using it.
Saving Data Using Shared Preferences
To save data using shared preferences, you can use the setter methods provided by the SharedPreferences object. These methods allow you to save different kinds of data such as strings, booleans, integers, doubles, and lists.
For example, if you want to store a boolean value, you will need to use the setBool() method. The syntax for this method is as follows:
prefs.setBool(String key, bool value)
Here, ‘key’ is used to identify the preference when you access it later. The ‘value’ is the boolean value you want to save.
For instance, if you want to save a boolean value ‘isLoggedIn’, you can do this with the following code:
prefs.setBool('isLoggedIn', true);
Similarly, you can use the setString(), setInt(), setDouble(), and setList() methods to save different data types.
Retrieving Saved Data
Once you have saved the data, you can use the getter methods provided by the SharedPreferences object to retrieve the saved values. These methods are used to retrieve different types of data such as strings, booleans, integers, doubles, and lists.
For example, if you want to retrieve the boolean value ‘isLoggedIn’ which you stored earlier, you can use the following code:
bool isLoggedIn = prefs.getBool('isLoggedIn');
Similarly, you can use the getString(), getInt(), getDouble(), and getList() methods to retrieve different data types.
Conclusion
In this tutorial, we have learned how to use shared preferences in Flutter. We saw how to save and retrieve different types of data such as strings, booleans, integers, doubles, and lists using the provided setter and getter methods. With this knowledge, you are now ready to start using shared preferences in your apps.