Flutter provides the shared_preferences
package, which allows you to store key-value data on a device. This can be useful for storing user preferences, session data, or other small pieces of data that need to be persisted across app sessions. In this article, we will take a deep dive into the shared_preferences
package, exploring its features and demonstrating how to use it with some examples.
Getting Started
To use the shared_preferences
package in your Flutter app, you’ll need to add it to your project’s pubspec.yaml
file:
dependencies:
flutter:
sdk: flutter
shared_preferences: ^2.0.7
Once you’ve added the shared_preferences
package to your project, you can import it in your code:
import 'package:shared_preferences/shared_preferences.dart';
Using SharedPreferences
Saving Data
To save data using shared_preferences
, you can use the SharedPreferences
class. To access the user’s shared preferences, you can use the getInstance
method. This method returns a Future
that resolves to an instance of the SharedPreferences
class.
Future<void> saveData() async {
final prefs = await SharedPreferences.getInstance();
await prefs.setString('username', 'john_doe');
await prefs.setInt('age', 35);
await prefs.setBool('is_logged_in', true);
}
In this example, we’re saving three different values: a string representing the user’s username, an integer representing their age, and a boolean indicating whether or not they’re currently logged in.
Retrieving Data
To retrieve data that you’ve saved using shared_preferences
, you can use one of the getter methods provided by the SharedPreferences
class. Here’s an example:
Future<void> retrieveData() async {
final prefs = await SharedPreferences.getInstance();
final username = prefs.getString('username') ?? 'no_username';
final age = prefs.getInt('age') ?? 0;
final isLoggedIn = prefs.getBool('is_logged_in') ?? false;
print('Username: $username');
print('Age: $age');
print('Logged in: $isLoggedIn');
}
In this example, we’re using the getString
, getInt
, and getBool
methods to retrieve the values we saved earlier. If the value isn’t found, we’re using the ??
operator to provide a default value.
Clearing Data
If you need to clear the data that you’ve saved using shared_preferences
, you can use the clear
method provided by the SharedPreferences
class. Here’s an example:
Future<void> clearData() async {
final prefs = await SharedPreferences.getInstance();
await prefs.clear();
}
In this example, we’re simply calling the clear
method to remove all of the data that we’ve saved using shared_preferences
.
Conclusion
The shared_preferences
package provides a simple and easy-to-use way to store key-value data on a device. Whether you’re saving user preferences or session data, this package can help you persist data across app sessions.
In this article, we’ve covered the basics of using shared_preferences
, including how to save, retrieve, and clear data. Hopefully, this has given you a good understanding of how to use this package in your own Flutter apps.