Flutter Sharedpreferences
Hello friends, in this article I will talk about how we can store small data while developing projects with Flutter, and how we can preserve it even if the application is closed.
What is SharedPreferences?
Normally, we use database structures such as mysql to store data in web applications, and sqlite for mobile or PC applications.Of course, using these databases is both laborious and unnecessary for small projects or systems with little data. That's why, by taking advantage of a function called Sharedpreferences, we can store our small data in a very simple way and use it whenever we want.
How to Use SharedPreferences?
We need to enter the pubspec.yaml file and add the shared_preferences: "Latest version" code block under dependencies: so that it becomes included in our project.dependencies:
flutter:
sdk: flutter
url_launcher: ^5.4.2
shared_preferences: ^0.5.6+2
After making this addition, we need to import SharedPreferences into the .dart pages where we will use SharedPreferences.
import 'package:shared_preferences/shared_preferences.dart';
After we complete the import part in our pages, we can now very easily save or fetch data to/from SharedPreferences.
Fetching data with SharedPreferences
sharedPreferencesFetchData() async{
SharedPreferences data = await SharedPreferences.getInstance();
int intData = data.getInt("Key");
double doubleData = data.getDouble("Key");
bool boolData = data.getBool("Key");
String stringData = data.getString("Key")
}
Friends, you can fetch the data you want with SharedPreferences in this way.
You can easily fetch the data using the keyword you entered when saving the data, like ".getInt("Key");".
Saving data with SharedPreferences
sharedPreferencesSaveData() async{
SharedPreferences data = await SharedPreferences.getInstance();
data.setInt("Key", 1);
data.setDouble("Key", 0.5);
data.setBool("Key", true);
data.setString("Key", "Your Text");
}
As you can see above, you can save your data in the form ".setInt("Key", value);". Here, "Key" is the keyword you will use to fetch the data in the future.
Note: Additionally, if you do not want the returned data to be Null, that is, if no data has previously been saved, you can append ?? 0 when fetching the data so the values that would return null will be shown as 0. In other words, you make a default value assignment.
Returning default values with SharedPreferences
sharedPreferencesSetDefault() async{
SharedPreferences data = await SharedPreferences.getInstance();
int intData = data.getInt("Key") ?? 0;
double doubleData = data.getDouble("Key") ?? 0.5;
bool boolData = data.getBool("Key") ?? true;
String stringData = data.getString("Key") ?? "Default Text";
}
Deleting data in SharedPreferences
If you want to delete any data, you can do so by using the ".remove("Key");" structure.sharedPreferencesDeleteData() async{
SharedPreferences data = await SharedPreferences.getInstance();
data.remove("Key");
}
That's all for using SharedPreferences; if you have anything you're curious about or have questions, you can ask below in the comments.


Yorum Gönder