What is Shared Preferences in Flutter?
Shared Preferences is a key-value storage mechanism that enables Flutter developers to store and retrieve data persistently on a device. It’s ideal for saving user preferences, configuration settings, or any lightweight data that needs to persist between app sessions.

Benefits of Using Shared Preferences
- Persistent Storage: Data stored using Shared Preferences remains available even after the app is closed.
- Lightweight: Best suited for small, simple datasets like user preferences or flags.
- Ease of Use: Provides a simple API for storing and retrieving data.
- Cross-Platform: Works seamlessly across Android and iOS.
Key Features of Shared Preferences
- Key-Value Pair Storage: Data is stored as a pair where each key corresponds to a specific value.
- Primitive Data Types: Supports basic types like
int
,double
,bool
,String
, andList<String>
. - Asynchronous Access: Uses asynchronous APIs to prevent UI blocking during read/write operations.
How to Use Shared Preferences in Flutter
1. Add the Dependency
To use Shared Preferences, include the plugin in your pubspec.yaml
file:
dependencies:
shared_preferences: ^2.1.0
Run flutter pub get
to install the package.
2. Import the Package
In your Dart file, import the shared_preferences
library:
import 'package:shared_preferences/shared_preferences.dart';
3. Save Data
To save data, use the set
methods available for each data type:
Future<void> saveData() async {
final prefs = await SharedPreferences.getInstance();
await prefs.setString('username', 'JohnDoe');
await prefs.setInt('age', 25);
await prefs.setBool('isLoggedIn', true);
}
4. Retrieve Data
To retrieve data, use the get
methods:
Future<void> retrieveData() async {
final prefs = await SharedPreferences.getInstance();
String? username = prefs.getString('username');
int? age = prefs.getInt('age');
bool? isLoggedIn = prefs.getBool('isLoggedIn');
print('Username: $username, Age: $age, Logged In: $isLoggedIn');
}
5. Remove Data
To delete specific data, use the remove
method:
Future<void> removeData() async {
final prefs = await SharedPreferences.getInstance();
await prefs.remove('username');
}
6. Clear All Data
To clear all stored data, use the clear
method:
Future<void> clearAllData() async {
final prefs = await SharedPreferences.getInstance();
await prefs.clear();
}
Best Practices for Using Shared Preferences
- Use for Small Data Only: Avoid using Shared Preferences for large datasets or complex data structures. For such cases, consider using a database like SQLite or Hive.
- Secure Sensitive Data: Shared Preferences is not encrypted. Avoid storing sensitive information like passwords or API keys. Use secure storage plugins for such purposes.
- Key Management: Use a consistent naming convention for keys to avoid conflicts. For example, prefix keys with the feature they belong to, like
auth_isLoggedIn
. - Error Handling: Always check for null values when retrieving data, as the key might not exist.
Common Use Cases for Shared Preferences
- User Authentication State: Save whether a user is logged in.
- App Themes: Store the user’s theme preference (dark/light mode).
- Language Settings: Persist the selected language across app launches.
- Introductory Screens: Track whether the user has seen the onboarding screens.
Limitations of Shared Preferences
- No Encryption: Data is stored in plain text, making it unsuitable for sensitive information.
- Limited Storage: Best suited for small data. Large datasets may slow down read/write operations.
- No Complex Data: Supports only primitive data types, requiring serialization for custom objects.
Conclusion
Shared Preferences is a powerful tool in Flutter for handling lightweight persistent storage. With its simple API and cross-platform compatibility, it’s an excellent choice for saving app settings and user preferences. By following the best practices outlined in this guide, you can effectively leverage Shared Preferences to enhance your app’s user experience.


FAQs
Q1: Can I store complex objects in Shared Preferences?
No, Shared Preferences supports only primitive data types. However, you can serialize complex objects to JSON strings and store them as String
.
Q2: Is Shared Preferences secure?
No, data stored in Shared Preferences is not encrypted. Use secure storage plugins like flutter_secure_storage
for sensitive data.
Q3: What is the size limit for Shared Preferences?
There’s no explicit size limit, but it’s designed for small amounts of data. Storing large data can impact performance.
Explore Other Flutter Topics…
- Introduction to Flutter and Dart
- Why choose Flutter
- Installing Flutter On Your Windows Mac And Linux System
- Your first Flutter app
- Flutter project structure
- Building blocks of Flutter
- Stateful vs. Stateless Widgets Explained
- Flutter layout system
- Flutter text widget
- Creating Buttons in Flutter: ElevatedButton, TextButton, and IconButton
- Handling User Input with Flutter Forms
- Container class in Flutter
- Flutter Navigation
- Flutter – Pass Data One Screen To Another Screen
- Managing Device Orientation in Flutter
- Stateful widget lifecycle in Flutter
- Future of Flutter
- Flutter Themes
- Flutter Animations
- Flutter AppBar Customization
- ListView in Flutter
- Flutter GridView
- Flutter Expanded Widget
- Flutter BottomNavigation Bar
- Floating Action Button
- Drawer Widgets in Flutter
- Form Validation in Flutter
- Flutter TextField
- Adding AdMob ads to a Flutter app
- Building Flutter Web & Desktop Applications
- What is Async and Await in Flutter
- HTTP requests in Flutter
- Parsing JSON in Flutter
- Tinder-Style Swipe Cards in Flutter
- Flutter Tic Tac Toe Game Tutorial
- Flutter Login UI Tutorial
- Flutter Card Widget Tutorial
- Flutter music player app tutorial
- Flutter introduction screens
- Shared Preferences in Flutter
- SQLite Database in Flutter
- Firebase Authentication in Flutter
- Firebase Firestore in Flutter
- Push Notifications in Flutter
- Handling File Uploads in Flutter
- Responsive Design in Flutter
- Provider in Flutter
- Riverpod in Flutter
- Flutter BLoC Pattern Tutorial