CHAPTER 20
Beginner
Local Storage and Shared Preferences
Updated: May 16, 2026
25 min read
# CHAPTER 20
Local Storage and Shared Preferences
1. Introduction
When a user toggles "Dark Mode" in your app, they expect the app to still be in Dark Mode when they open it the next day. However, standard variables (likebool isDark = true) are stored in RAM. When the app is closed, RAM is wiped clean, and the variable resets. To remember settings permanently, you must write data directly to the phone's physical hard drive. In this chapter, we will master Local Storage and Shared Preferences. We will install the industry-standard package to easily read, write, and delete small pieces of data (like high scores, login tokens, and user preferences) so your app remembers who the user is.
2. Learning Objectives
By the end of this chapter, you will be able to:-
Install and initialize the
sharedpreferencespackage.
- Write Strings, Integers, and Booleans to the device's local storage.
- Read stored data asynchronously on app startup.
- Delete or clear stored preferences.
- Understand the limitations of Shared Preferences vs a true database.
3. What are Shared Preferences?
sharedpreferences is an official Flutter package that wraps the native storage systems of the operating system (Android SharedPreferences and iOS NSUserDefaults).
It stores data in simple Key-Value pairs.
-
Key:
"username"-> Value:"Alice"
-
Key:
"highscore"-> Value:450
*Important: It is designed for SMALL amounts of data (under a few megabytes). Do not try to store 10,000 chat messages here.*
4. Installation and Setup
-
1.
Add the package to your
pubspec.yaml:
sharedpreferences: ^2.2.0
- 2. Import it in your Dart file:
import 'package:sharedpreferences/sharedpreferences.dart';
5. Saving Data (Writing)
Writing to the hard drive is a physical disk operation, which means it takes time. Therefore, we must useasync and await!
dart
*(You can also use .setString, .setBool, and .setDouble depending on your data type).*
6. Loading Data (Reading)
When the app launches, you want to grab the saved score to display it. If the app has never been opened before, the data won't exist! You must provide a fallback value (like 0).
dart
7. Deleting Data
When a user clicks "Log Out", you must delete their stored login token from the hard drive.
dart
8. Mini Project: The Remember Me Nameplate
Let's build a UI that asks for a name, saves it, and displays it automatically the next time the app opens.
dart
9. Common Mistakes
-
Storing Sensitive Data:
sharedpreferencessaves data in plain text XML files on the phone. Do NOT store raw passwords, credit card numbers, or secure encryption keys here! A rooted/jailbroken phone can easily read these files. For sensitive data, use thefluttersecurestoragepackage instead.
10. Best Practices
-
Define Keys as Constants: If you misspell
'username'as'username'when trying to read the data, it will return null. Always define your keys as constants at the top of your file:const String KEYNAME = 'username';and useprefs.getString(KEYNAME)to eliminate typo bugs.
11. Practice Exercises
- 1. What official Flutter package is used to save simple Key-Value pairs to the device's local hard drive?
-
2.
Why is the
awaitkeyword mandatory when callingSharedPreferences.getInstance()?
12. MCQs with Answers
Question 1
A developer wants to save an offline catalog of 5,000 product objects including titles, descriptions, and prices. Why is sharedpreferences a poor choice for this task?
Question 2
When attempting to read a setting via prefs.getBool('isDarkMode'), what does the method return if the user has never opened the app before and the key does not exist?
13. Interview Questions
-
Q: Explain the purpose of
sharedpreferencesin the Flutter ecosystem. Provide three examples of data perfectly suited for this package.
-
Q: Contrast
sharedpreferenceswithfluttersecurestorage. When is it absolutely mandatory to use the latter?
-
Q: Describe how you would utilize the
initStatemethod in aStatefulWidgetto seamlessly load and display a user's saved preferences exactly when the app launches.
14. FAQs
Q: Can I save a custom Object (like aUser class) into Shared Preferences?
A: Not directly. You can only save strings. You must encode your User object into a JSON String using jsonEncode(), save the string, and then jsonDecode() it when you read it back.
15. Summary
In Chapter 20, we gave our application a permanent memory. We moved beyond temporary RAM variables and utilized theshared_preferences package to write data directly to the device's physical hard drive. We mastered the asynchronous workflow required for disk operations, writing data using .setString() and reading it smoothly upon app launch using .getString() combined with initState. We learned to define fallback values using ?? to prevent null errors, and established the security rules of what *not* to store in plain text.