SharedPreferences in Flutter
SharedPreferences is a helpful tool for storing a small amount of information in Flutter projects. It works by saving and getting simple data like numbers, words, and true/false statements in a special file on the device. For instance, you can use it to remember if someone is logged into an app, so they don’t have to type their password every time. In Android apps, you can store whether someone is logged in using a “yes” or “no” statement, which is easy and quick to use, unlike a big database. This makes things faster and simpler for the user.
How to use SharedPreferences in Flutter
Using SharedPreferences in Flutter is relatively straightforward and involves a few steps.
Step 1 : Add Dependencies:
Make sure you have the shared_preferences package added to your pubspec.yaml file. You can do this by adding the following line under the dependencies section:
dependencies:
flutter:
sdk: flutter
shared_preferences: ^2.2.0
After adding this, run the command ‘flutter pub get’
Step 2 : Import Package
Now open the file where we want to use SharedPreferences. And import the package of class sharedpreferences.
import 'package:shared_preferences/shared_preferences.dart';
Step 3: Save data in to SharedPreferences in Flutter
We can save these types of data in SharePreferences in Flutter.
- Int
- Double
- String
- Boolean
Data can be saved in form of key value pair in SharedPreferences. The key always will be the string value. And value of this key can be one of the above mentioned data types.
There are setters and getters in SharedPreferences class that we can use to save and retrieve our data in SharedPreferences. These setters take two parameters to save data. First parameter will be the key and second parameter will be the value ( which we want to store in SharedPreferences).
Save String in SharedPreference
saveStringToSharedPreferences() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString('stringValue', "abc");
}
Here we can see that we have passed two parameters in setString method. First parameter is key, and second parameter is value. These both are strings. Because we are saving a string in SharedPreferences.
Save Int in SharedPreferences
saveIntToSharedPreferences() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setInt('intValue', 123);
}
Here we have passed two parameters. First one is string and second one is int. Because we are saving interger value in SharedPreferences.
Save double in SharedPreferences
savedoubleToSharedPreferences () async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setDouble('doubleValue', 115.0);
}
Save Boolean in SharedPreferences
addBoolToSF() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setBool('boolValue', true);
}
Retrieve data from SharedPreferences
When we retrieve data from storage using SharedPreferences, we only need to provide the specific key.
getStringValuesSF() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
String stringValue = prefs.getString('stringValue');
return stringValue;
}
getBoolValuesSF() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
bool boolValue = prefs.getBool('boolValue');
return boolValue;
}
getIntValuesSF() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
int intValue = prefs.getInt('intValue');
return intValue;
}
getDoubleValuesSF() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
double doubleValue = prefs.getDouble('doubleValue');
return doubleValue;
}
Null Value in SharedPreferences
It might be possible that SharedPreferences does not contain that value which we are trying to retrieve from this. Then SharedPreferences will return Null value. We can handle this null value like this
Future<int> getStringValue() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
int intValue = prefs.getInt('key') ?? 0;
return intValue;
}
Here we check that if retuned value is null, then we return 0;
Check if Value is present or not?
We can also check if that is value exists in SharedPreferences or not.
bool checkValue = prefs.containsKey('key') ;
Here we used containskey method. Which return true if value exists in sharedPreferences, otherwise it will return false.
Remove values from SharedPreferences
We can also remove values from SharedPreferences, if we do not required that values anymore. To do this we just use remove method like this.
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.remove('key') ;
This will remove data from SharedPreferences.