Skip to main content

How to user Shared Preferences in Flutter

As we know Shared Preference is used to store primitive data like string, int, double, boolean and list of string.

Shared Preferences data are stored within the app so all the data will be cleared if the user uninstalls an application or clear the storage data from Application info.

To use shared preference in Flutter you have to get the plugin named "shared_preferences: which was published by "flutter.dev"
you can check more detail here


Shared Preference in Flutter

To get this, you have to write shared_preferences: version to pubspec.yaml like below mentioned.   

dependencies:
  flutter:
    sdk: flutter
  shared_preferences: ^0.4.3
 
You can change the version number to the latest.

Now you need to import the package in the file you want to use shared preference

import 'package:shared_preferences/shared_preferences.dart';

Reading and writing data to shared preference

First, you need to get the object of Shared Preference like below

final prefs = await SharedPreferences.getInstance();

after that, you just have to read/write data according to your requirement

Here are some examples of different primitive data

int
read: final myInt = prefs.getInt('my_int_key') ?? 0;
write: prefs.setInt('my_int_key', 42);

double
read: final myDouble = prefs.getDouble('my_double_key') ?? 0.0;
write: prefs.setDouble('my_double_key', 3.14);

bool
read: final myBool = prefs.getBool('my_bool_key') ?? false;
write: prefs.setBool('my_bool_key', true);

string
read: final myString = prefs.getString('my_string_key') ?? '';
write: prefs.setString('my_string_key', 'hello');

stringList
read: final myStringList = prefs.getStringList('my_string_list_key') ?? [];
write: prefs.setStringList('my_string_list_key', ['horse', 'cow', 'sheep']);


That's It.

Comments

Popular posts from this blog

Tri-States | Indeterminate Checkbox Android

Sometimes it is required to implement checkbox with 3 states Unchecked Checked Indeterminate So here is a simple example of how to implement Tri-States or you can say Indeterminate Checkbox Android First of all, make a custom class which is extend Checkbox import android.content.Context import android.util.AttributeSet import androidx.appcompat.widget.AppCompatCheckBox import com.android.tristatescheckbox.R class TriStatesCheckBox : AppCompatCheckBox { private var state = 0 constructor ( context: Context ?) : super ( context ) { init () } constructor ( context: Context ?, attrs: AttributeSet ?) : super ( context , attrs ) { init () } constructor ( context: Context ?, attrs: AttributeSet ?, defStyleAttr: Int ) : super ( context , attrs , defStyleAttr ) { init () } private fun init () { state = UNCHECKED...

API Level in Android

Android Version Released API Level Name Build Version Code Android 7.1 December 2016 25 Nougat Android.OS.BuildVersionCodes.NMr1 Android 7.0 August 2016 24 Nougat Android.OS.BuildVersionCodes.N Android 6.0 August 2015 23 Marshmallow Android.OS.BuildVersionCodes.M Android 5.1 March 2015 22 Lollipop Android.OS.BuildVersionCodes.LollipopMr1 Android 5.0 November 2014 21 Lollipop Android.OS.BuildVersionCodes.Lollipop Android 4.4W June 2014 20 Kitkat Watch Android.OS.Bui...

Change Font type of Tab in TabLayout in Android | Android Studio

Here is the example of how to change the font of the tab in Tab layout in android You can simply change the font type of tab Tab layout and you can also set the custom font To change the font of tab layout, you have to paste the font in assets folder of your project You just have to follow the one method in an existing project or for the whole example, you can refer the following code.