Flutter Shared Preferences: Saving and Loading Data Locally

Geno Tech
Geno Tech
Apr 21 · 3 min read

Flutter Knowledge Sharing #18

In the Flutter development journey, we have discussed 18 topics up to now. This is another common topic everyone should know as a mobile developer. Shared preferences used in your app for different purposes and very useful to know. Shared Preferences store the local data as key-value pairs. You can use those data anywhere in your application. These data will clear when uninstalling the application or when clearing the application data. The main purpose of this is to store user-specified configuration details. Such as user settings, login details, ids, etc. This story will discuss the following steps.

  1. Add the Shared Preferences Dependency
  2. Save a Shared Preferences
  3. Load a Shared Preferences
  4. Delete a Shared Preferences

1. Add the Shared Preferences Dependency

First, you must add the dependencies to pubspec.yaml.

2. Save a Shared Preferences

Then I will show how to save the shared preferences as key-value pairs. Declare a new SharedPreferences object and initialize the object.

Then we save data using the pref object. Here I show you how we save different types of variables. Here we allow using these five variable types(Int, String, Double, Boolean, List). As the first parameter, we send the key and the second parameter is the value.

3. Load a Shared Preferences

Let us see how we fetch the saved data. Here I gave default values if the retrieving value is not present or return an error. Finally, the results print in the log.

4. Delete a Shared Preferences

We can delete existing shared preferences using its key as follows.

Full Code

This is the main file of our application, this main.dart has three buttons to store, fetch and delete local data. You can see the functionalities of those buttons.

Conclusion

Here I tried to give you a complete idea about how to save and load Shared Preferences in Flutter. I hope you got amazing knowledge to use in your next Flutter project. Please feel free to ask any question you will face in the response section below.
Happy Coding !!!!
Found this post useful? Kindly tap the 👏 button below! :)

App Dev Community

This is for Mobile App Developer to exploring and sharing the knowledge.

Geno Tech

Written by

Geno Tech

Mobile I Web I Data Science I AI — We write rich & meaningful content on development, technology, digital transformation & life lessons.

App Dev Community

A group of mobile app developers sharing the knowledge

Geno Tech

Written by

Geno Tech

Mobile I Web I Data Science I AI — We write rich & meaningful content on development, technology, digital transformation & life lessons.

App Dev Community

A group of mobile app developers sharing the knowledge

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store