Efficient Data Management in Flutter with Shared Preferences
Shared Preferences is a simple and easy-to-use package in Flutter that allows you to store and retrieve data on the device.
In this post, we'll explore how Shared Preferences works and how to use it to save and retrieve data in your Flutter app.
We'll cover the basics of storing and retrieving data types such as booleans, integers, doubles, strings, and lists of strings. Additionally, we'll show you how to remove and clear data from Shared Preferences.
With Shared Preferences, you can simplify your data persistence needs and provide a better user experience in your app.
Step 1: Add dependencies
First, you need to add the Dio and Provider packages to your Flutter project by adding the following lines to your pubspec.yaml file:
shared_preferences: ^2.0.18
..
Step 2: Create the Common util class :
Now, create a new file called shared_preferences.dart and add the following code:
import 'package:shared_preferences/shared_preferences.dart';
class SharedPrefUtils {
// Saves a boolean value with the given key to SharedPreferences
static Future<bool> saveBool(String key, bool value) async {
final prefs = await SharedPreferences.getInstance();
return prefs.setBool(key, value);
}
// Saves an integer value with the given key to SharedPreferences
static Future<bool> saveInt(String key, int value) async {
final prefs = await SharedPreferences.getInstance();
return prefs.setInt(key, value);
}
// Saves a double value with the given key to SharedPreferences
static Future<bool> saveDouble(String key, double value) async {
final prefs = await SharedPreferences.getInstance();
return prefs.setDouble(key, value);
}
// Saves a string value with the given key to SharedPreferences
static Future<bool> saveString(String key, String value) async {
final prefs = await SharedPreferences.getInstance();
return prefs.setString(key, value);
}
// Saves a list of strings with the given key to SharedPreferences
static Future<bool> saveStringList(String key, List<String> value) async {
final prefs = await SharedPreferences.getInstance();
return prefs.setStringList(key, value);
}
// Retrieves a boolean value with the given key from SharedPreferences
static Future<bool?> getBool(String key) async {
final prefs = await SharedPreferences.getInstance();
return prefs.getBool(key);
}
// Retrieves an integer value with the given key from SharedPreferences
static Future<int?> getInt(String key) async {
final prefs = await SharedPreferences.getInstance();
return prefs.getInt(key);
}
// Retrieves a double value with the given key from SharedPreferences
static Future<double?> getDouble(String key) async {
final prefs = await SharedPreferences.getInstance();
return prefs.getDouble(key);
}
// Retrieves a string value with the given key from SharedPreferences
static Future<String?> getString(String key) async {
final prefs = await SharedPreferences.getInstance();
return prefs.getString(key);
}
// Retrieves a list of strings with the given key from SharedPreferences
static Future<List<String>?> getStringList(String key) async {
final prefs = await SharedPreferences.getInstance();
return prefs.getStringList(key);
}
// Removing data from SharedPreferences
static Future<void> removeData(String key) async {
final prefs = await SharedPreferences.getInstance();
await prefs.remove(key);
}
// Clearing all data from SharedPreferences
static Future<void> clearData() async {
final prefs = await SharedPreferences.getInstance();
await prefs.clear();
}
}
..
Step 3: Call the function you need :
To use the functions in this SharedPrefUtils class, you can follow these steps:
For example, to save a boolean value to SharedPreferences, you can call the saveBool function:
bool valueToSave = true;
String key = "myBooleanKey";
SharedPrefUtils.saveBool(key, valueToSave);
..
To retrieve the saved value, you can call the corresponding function:
String key = "myBooleanKey";
bool? retrievedValue = await SharedPrefUtils.getBool(key);
if (retrievedValue != null) {
// Do something with the retrieved value
} else {
// The value was not found in SharedPreferences
}
..
To remove a value from SharedPreferences, you can call the removeData function:
String keyToRemove = "myBooleanKey";
await SharedPrefUtils.removeData(keyToRemove);
..
To clear all data from SharedPreferences, you can call the clearData function:
await SharedPrefUtils.clearData();
..
Note that all functions in this class are asynchronous, so you need to use the await keyword when calling them.
For example:
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import '../helper/shared_preferences.dart';
class UserList extends StatefulWidget {
const UserList({super.key});
@override
State<UserList> createState() => _UserListState();
}
class _UserListState extends State<UserList> {
Future<void> userDetails() async {
// Save a boolean value to SharedPreferences
await SharedPrefUtils.saveBool('is_logged_in', true);
// Retrieve a boolean value from SharedPreferences
final isLoggedIn = await SharedPrefUtils.getBool('is_logged_in');
if (kDebugMode) {
print("output : $isLoggedIn");
}
}
@override
void initState() {
super.initState();
userDetails();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('API List view'),
),
);
}
}
..
Comments
Post a Comment