Shared Preferences in Flutter

Vijay R
vijaycreations
Published in
2 min readOct 11, 2020

Store the data locally in Flutter app using Shared Preferences.

Shared Preferences is a kind of local storage that we can make use of to store smaller data inside Flutter App. The data in Shared Preferences are stored in the form of key value pair.

Video Tutorial

Dependencies

shared_preferences: ^0.5.12

Implementation

In this article we shall discuss how to store, retrieve and delete the data using Shared Preferences.

  1. Storing data:

Let me store a String datatype locally using the shared preferences concept.

call the above defined function by passing the data <String> that we want to store locally saveMyName(“myName”) Use setString() to store the data. The setString() accepts the key and the Value . In the above code snippet the name enclosed within codes "name" is the key and the second parameter name is the value.

2. Retrieve data:

Now let’s retrieve the data back what we have stored before.

For Retrieving the data we need to just pass in the key alone. In our case the Key is name . So we have to pass that to get the data back [Line no:4] from Shared Preferences. “Name not found[Line no:4] is a custom message that we show in case if there is no entry for that particular key in the local storage.

3. Deleting the data:

For deleting the particular entry, make use of remove method and pass the key alone as the parameter.

Just like String we can also store Integer and Bool datatypes in Shared Preferences.

For Integer use., getInteger() setInteger()

For Boolean use., getBool() setBool()

👨‍💻Get the complete Source Code here 👉🏻: https://github.com/vijayinyoutube/sharedpreferences

--

--

Vijay R
vijaycreations

Hai👋 I’m a flutter developer experienced in designing and developing stunning mobile apps. Reach out for freelance projects: calico.takeoff_01@icloud.com