Skip to content
Related Articles

Related Articles

How to Install Shared Preferences in Flutter?

View Discussion
Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 25 Mar, 2022

Shared preferences in flutter applications are used to store data in Local Storage. Shared Preferences store the data in a key-value pair. and we can also get data in a key-value pair using inbuilt functions.

We can save large data in local storage or our Phone storage with the help of SQLite database but instead of if we want to save a small amount of data in local storage, then we should use shared preferences instead of SQLite database. In SharedPreferences, we have to give just a key for every single value so we can get that value using this same key. In this article, we will look at “How to Install shared_preferences package in Flutter”. To do so follow the below steps:

Method 1:  Simply from the  command line of your flutter project :

  • Run this command:

flutter pub add shared_preferences

This will add a line like this to your package’s pubspec.yaml (and run an implicit dart pub get):

Current-version-of-package

Note: This will add the current version of the package.

Method 2: By adding explicitly to your package’s pubspec.yaml:

Step 1: Just visit the flutter site.

Visiting-flutter

Step 2: Now copy the package as shown in the above image. Go to the pubspec.yaml in your flutter project.

Selecting-pubspec.yaml

Step 3: Under the dependencies section below paste the Plugin.

Pasting-plugin

Step 4: Run an implicit dart pub get

Running-implicit-dart-pub-get

Now we successfully Install the Shared_preferences Plugin.

Import it

Now in your Dart code, you can use:

import ‘package:shared_preferences/shared_preferences.dart’;

Congratulations you did it.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!