0% found this document useful (0 votes)
6 views

SharedPreferences in Flutter

SharedPreferences in Flutter is a simple key-value storage solution for storing primitive data types like String, int, and bool locally on a device. It is ideal for saving user preferences, session states, and small settings data, but is not suitable for large datasets. Best practices include using it for small data, avoiding sensitive information without encryption, and managing read/write efficiency.

Uploaded by

Zahraa A Genedy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

SharedPreferences in Flutter

SharedPreferences in Flutter is a simple key-value storage solution for storing primitive data types like String, int, and bool locally on a device. It is ideal for saving user preferences, session states, and small settings data, but is not suitable for large datasets. Best practices include using it for small data, avoiding sensitive information without encryption, and managing read/write efficiency.

Uploaded by

Zahraa A Genedy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

SharedPreferences in Flutter: A Simple Local Storage Solution

Introduction
In mobile app development, it's often necessary to store simple data locally on the device, such as
user preferences, session details, or small amounts of data that don’t need a database. Flutter offers a
solution for this with SharedPreferences, a key-value storage system that stores data in the form of
key-value pairs. It’s an easy and efficient way to persist simple data locally.
What is SharedPreferences?
SharedPreferences is a simple key-value pair storage system that allows you to store primitive data
types like String, int, bool, and double. This is commonly used to store user preferences or app
settings.
How Does SharedPreferences Work?
The SharedPreferences plugin allows Flutter apps to save data locally. It works by saving data into a
persistent file that the app can access anytime. It’s simple and lightweight, making it ideal for storing
small pieces of data such as:
• User preferences (e.g., dark/light theme)
• Login session states
• Onboarding completion status
• Last visited screen or page
• Any small settings data
Setting Up SharedPreferences in Flutter
To use SharedPreferences in your Flutter app, you need to follow these steps:
1. Add Dependency
First, you need to add the shared_preferences package to your pubspec.yaml file:
yaml
‫نسختحرير‬
dependencies:
shared_preferences: ^2.0.15 # Check for the latest version
Run the command to get the package:
bash
‫نسختحرير‬
flutter pub get
2. Import the Package
In the Dart file where you want to use SharedPreferences, import the package:
dart
‫نسختحرير‬
import 'package:shared_preferences/shared_preferences.dart';
3. Using SharedPreferences
Now, let's go through some examples of how to save, retrieve, and remove data.
Saving Data
To save a value, you use the set methods. Here’s an example of saving a String:
dart
‫نسختحرير‬
void saveData() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setString('username', 'Zahraa');
await prefs.setInt('userAge', 25);
await prefs.setBool('isLoggedIn', true);
}
Retrieving Data
To retrieve a value, use the get methods:
dart
‫نسختحرير‬
void getData() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
String? username = prefs.getString('username');
int? age = prefs.getInt('userAge');
bool? isLoggedIn = prefs.getBool('isLoggedIn');

print('Username: $username');
print('Age: $age');
print('Is Logged In: $isLoggedIn');
}
Removing Data
To remove data from SharedPreferences, use the remove method:
dart
‫نسختحرير‬
void removeData() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.remove('username');
}
Clear All Data
If you want to clear all stored data, you can use the clear method:
dart
‫نسختحرير‬
void clearData() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.clear();
}
Use Cases of SharedPreferences
• User Settings: Saving theme preferences, language settings, or notification preferences.
• Login Data: Storing the user’s login state (logged in or not) or tokens for authenticated sessions.
• App State: Remembering the last page or tab a user visited, app tutorial completion, etc.
Pros and Cons of SharedPreferences
Pros:
• Simple and easy to use.
• Efficient for storing small pieces of data.
• No need for a database setup.
Cons:
• Not suitable for large datasets (e.g., images, large objects).
• Can be prone to data loss in case of app uninstallation or if the app data is cleared.
Best Practices
1. Data Size: Use SharedPreferences only for storing small pieces of data. For larger datasets,
consider using a database like SQLite or Hive.
2. Security: Do not store sensitive data (like passwords or tokens) in SharedPreferences without
proper encryption. Use a more secure storage solution if necessary (e.g., Flutter Secure Storage).
3. Read and Write Efficiency: Avoid reading and writing to SharedPreferences excessively. Instead,
cache values locally and sync them periodically.
Conclusion
SharedPreferences is a lightweight and efficient solution for storing simple data locally on a device. It’s
ideal for saving user settings, app states, or session data. While it’s easy to implement, be mindful of
its limitations regarding the size of data and security concerns. For more complex data storage needs,
consider using databases or more secure storage solutions.

You might also like