Best Way To Use SharedPreferences In Flutter
In Flutter’s mobile application, if we have to store some value permanently in the mobile device itself, while the user does not clear the data of the application, then our first choice is to use shared_preferences . We know that shared_preferences makes this work easy for us, but we can store many types of data in shared_preferences as we use in our application, Anyway I am going to tell you in this article that we can use shared_preferences in a better way. how can use.
So Let’s start
I hope that you must have installed the required plugin to use shared_preferences and if you haven’t, then I am giving you the link below, you can install it.
shared_preferences So you click on this link and add the plugin to your pubspec.yaml file.
I assume that you must have added your plugin, then you have to create a dart file, you can give the name of the file whatever you like. Here I name the file shared_preferences.dart.
1. Method :- Basic use
simply Basic syntax to use stringlist in shared preferences.
// for read
final myStringList = prefs.getStringList('string_list_key') ?? [];
// for write
prefs.setStringList('my_string_list_key', ['abc', 'xyz', 'pqr']);
2. Method : – Using Class
shared_preferences.dart
In this file, we are going to create a class and the file will contain all the methods that we will need for shared_preferences.
import 'package:shared_preferences/shared_preferences.dart';
class ProjectSharedPreferences {
ProjectSharedPreferences._privateConstructor();
static final ProjectSharedPreferences instance =
ProjectSharedPreferences._privateConstructor();
setStringValue(String key, String value) async {
SharedPreferences myPrefs = await SharedPreferences.getInstance();
myPrefs.setString(key, value);
}
Future<String> getStringValue(String key) async {
SharedPreferences myPrefs = await SharedPreferences.getInstance();
return myPrefs.getString(key) ?? "";
}
setIntegerValue(String key, int value) async {
SharedPreferences myPrefs = await SharedPreferences.getInstance();
myPrefs.setInt(key, value);
}
Future<int> getIntegerValue(String key) async {
SharedPreferences myPrefs = await SharedPreferences.getInstance();
return myPrefs.getInt(key) ?? 0;
}
setBooleanValue(String key, bool value) async {
SharedPreferences myPrefs = await SharedPreferences.getInstance();
myPrefs.setBool(key, value);
}
Future<bool> getBooleanValue(String key) async {
SharedPreferences myPrefs = await SharedPreferences.getInstance();
return myPrefs.getBool(key) ?? false;
}
Future<bool> containsKey(String key) async {
SharedPreferences myPrefs = await SharedPreferences.getInstance();
return myPrefs.containsKey(key);
}
removeValue(String key) async {
SharedPreferences myPrefs = await SharedPreferences.getInstance();
return myPrefs.remove(key);
}
setListData(String key, List<String> value) async {
SharedPreferences myPrefs = await SharedPreferences.getInstance();
myPrefs.setStringList(key, value);
}
getListData(String key) async {
SharedPreferences myPrefs = await SharedPreferences.getInstance();
return myPrefs.getStringList(key);
}
removeAll() async{
SharedPreferences myPrefs = await SharedPreferences.getInstance();
return myPrefs.clear();
}
}
Now let’s see how to use it.
Set data in shared_preferences .
Here I have given an example but for whatever method you want, you just have to call that method ( Like I used here setListData and getListData method ) and pass whatever parameter you want, in the key you can give your variable name in which you have to store data and You have to pass the same key at the time of fetch data so that you will get the same data.
//Key : Set variable name for shared_prefrences
ProjectSharedPreferences.instance.setListData("key ",value);
// value : set value what you want to store
Get Data From shared_preferences
//
List dataset= [];
await ProjectSharedPreferences.instance
.getListData("key")
.then((value) => setState(() {
dataset = value;
}));
//