Enable Dark Mode!
how-to-use-shared-preferences-in-flutter.jpg
By: Shabeer PT

How to Use Shared Preferences in Flutter

Flutter

What is SharedPreferences?

SharedPreferences is only used to store the user's small amount of data within the application, such as settings and data (not too much; if too much, then a database is required).
SharedPreferences in Flutter are kept in XML format. It is also compatible with iOS and Android.
Assume you want to store a small value (likely a flag) that you want to be able to access later when a user opens the application. Shared preference then takes effect.
For small-value storage, we do not use SQLite since doing so requires writing complex code and supporting classes.

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.

How to use SharedPreferences in Flutter?

You should be aware that the Flutter SDK does not support SharedPreferences before using them, but thankfully, you can use the shared_preferences plugin to store key-value data on the disc.

Implementation

Step 1: Adding the dependencies

We need to install the required package before we can start using SharedPreferences. Using pubspec.yaml in our dart project, we will open it for this purpose and add the following line under the "dependencies" section:

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

Following the modifications, there are two ways we can install the package:

To obtain packages, right-click on the pubspec.yaml file and select "get packages."

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 the trouble of going through each of the previously mentioned steps.

flutter pub add shared_preferences

Step 2: Import shared_preferences.dart

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

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

Saving int value

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

Saving double value

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

Saving boolean value

addBoolToSF() 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

getStringValuesSF() 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");
}

Check value if present or not:

SharedPreferences prefs = await SharedPreferences.getInstances.getInstance();
Bool checkValue = prefs.containsKey('value');

If the given key is present in persistent storage, containsKey will return true; otherwise, it will return false.

We can efficiently use your Flutter app's Shared Preferences to store and retrieve small amounts of data over time by following these steps. It's critical for controlling user preferences and settings, improving the user experience as a whole, and designing an intuitive user interface for your application.


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



whatsapp
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message