Open In App

How to Install Shared Preferences in Flutter?

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 :

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):



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.

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

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

Step 4: Run an 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.

Article Tags :