Do somthings

“In a world where technology is constantly evolving, understanding how to write efficient code is more important than ever.”

Hello, everyone! I hope you’re all doing well. Today, we’re diving into an exciting topic—learning how to implement x in Flutter. So first we understand what is Shared Preferences.

How to Use Shared Preferences in Flutter

What is SharedPreferences?

The user’s modest quantity of data, such as settings and data (not too much; if too much, then a database is required), is the only thing that is stored in SharedPreferences within the application.
Flutter stores SharedPreferences in an XML format. It works with both iOS and Android as well.
Let’s say you have a little value (perhaps a flag) that you would like to save and retrieve when the user launches the program. Then, shared preference comes into play.
We don’t use SQLite for small-value storage since it necessitates the creation of intricate code and supporting classes.

How to use SharedPreferences in Flutter?

Before using SharedPreferences, be advised that the Flutter SDK does not support them. Fortunately, you may save key-value data on the disc by utilizing the shared_preferences plugin.

Implementation

Step 1: Adding the dependencies

Before we can use SharedPreferences, we must install the necessary package. For this purpose, we will open pubspec.yaml in our dart project and add the following line under the “dependencies” section:

dependencies:
 flutter:
   sdk: flutter
 shared_preferences: "<newest version>"

Insert the subsequent command into the terminal:

flutter pub get

The shared_preferences package will be automatically updated when we execute the following command, saving us from going through each of the previously mentioned steps.

Step 2: Import shared_preferences.dart

If you want to use shared preferences, you just need to import them at the top of the file, like:

import 'package:shared_preferences/shared_preferences.dart';

Step 3: Save data

With SharedPreferences, we can only add int, String, double, and bool.

The SharedPreferences class has setter methods that accept the parameter’s key and value.

Saving String value:

addString() async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  prefs.setString('stringValue', "abc");
}

Saving int value:

addInt() async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  prefs.setInt('intValue', 123);
}

Saving double value:

addDouble() async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  prefs.setDouble('doubleValue', 115.0);
}

Saving boolean value:

addBool() async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  prefs.setBool('boolValue', true);
}

Step 4: Read data

We only need to pass the key when using SharedPreferences to read data from the storage.

Read string value:

getStringValues() async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  //Return String
  String stringValue = prefs.getString('stringValue');
  return stringValue;
}

Read boolean value:

getBoolValuesSF() async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  //Return bool
  bool boolValue = prefs.getBool('boolValue');
  return boolValue;
}

Read int value:

getIntValuesSF() async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  //Return int
  int intValue = prefs.getInt('intValue');
  return intValue;
}

Read double value:

getDoubleValuesSF() async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  //Return double
  double doubleValue = prefs.getDouble('doubleValue');
  return doubleValue;
}

We might receive a null value if the value is not present in the storage.

We can use it to deal with this.

int intValue= await prefs.getInt('intValue') ?? 0;

Step 5: Remove data:

We can add the key to the .remove(String key) method to remove the data from the storage.

removeValues() async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  //Remove String
  prefs.remove("stringValue");
  //Remove bool
  prefs.remove("boolValue");
  //Remove int
  prefs.remove("intValue");
  //Remove double
  prefs.remove("doubleValue");
}

Step 5: Edit data:

you can also edit the save data but it is the same process and syntax like .set keyword.

editValues() async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  //Edit String
  prefs.set("stringValue");
  //Edit bool
  prefs.set("boolValue");
  //Edit int
  prefs.set("intValue");
  //Edit double
  prefs.set("doubleValue");
}

Conclusion:

By taking these steps, we may effectively save and retrieve modest quantities of data over time using the Shared Preferences of your Flutter app. Controlling user preferences and settings, enhancing the overall user experience, and creating an application with an intuitive user interface all depend on it.

Note

* The purpose of SharedPreference is to store user data within the app.
* Deleting application data or uninstalling an app, shared preference data will be lost.
* SharedPreference should not be used as a database.
With SharedPreference, key-value pairs can be easily read and written in a few lines. Remember, though, that SharedPreferences is not a way for you to store complex relational data.

❤️❤️ Thanks for reading this article ❤️❤️

If I got something wrong? Let me know in the comments. I would love to improve 🥰🥰🥰.

Clap 👏👏👏 If this article helps you,

if you like our work, please follow us on this Dosomthings

Our more attractive articles:

Introduction Screens: E-Commerce App UI in Flutter | EP.01 Introduction Screens.

Refresh in Flutte: How to implement Pull to Refresh in Flutter?

Onboarding Screen: Onboarding Screen in Flutter.