What if your app could remember user choices instantly without complex code?
Why SharedPreferences for key-value in Flutter? - Purpose & Use Cases
Imagine you want your app to remember a user's name or theme choice every time they open it. Without a simple way to save this small info, you'd have to ask the user again and again, which feels annoying and unprofessional.
Trying to save data manually means writing complex code to store files or databases just for tiny bits of info. This is slow, easy to mess up, and wastes time that could be spent making your app better.
SharedPreferences lets you save and get small pieces of data like strings or numbers easily. It works like a tiny notebook your app can quickly read from or write to, so user choices stay saved without hassle.
File file = File('user.txt'); await file.writeAsString('username=John'); String name = file.readAsStringSync();
final prefs = await SharedPreferences.getInstance(); await prefs.setString('username', 'John'); String? name = prefs.getString('username');
It makes saving user settings and preferences simple, fast, and reliable, improving user experience instantly.
Think about a weather app that remembers your city so you don't have to type it every time you open it. SharedPreferences makes that easy.
Manual saving is slow and error-prone for small data.
SharedPreferences offers a quick way to store key-value pairs.
This improves app usability by remembering user choices.
Practice
SharedPreferences in a Flutter app?Solution
Step 1: Understand SharedPreferences role
SharedPreferences is designed to store simple data like settings or flags in key-value pairs.Step 2: Compare with other options
Options A, B, and C relate to databases, UI, and networking, which are not the purpose of SharedPreferences.Final Answer:
To save small key-value data that persists after the app closes -> Option AQuick Check:
SharedPreferences = persistent small key-value storage [OK]
- Confusing SharedPreferences with databases
- Thinking it manages UI or network
- Assuming it stores large files
true with key isLoggedIn using SharedPreferences in Flutter?Solution
Step 1: Recall SharedPreferences method names
The correct method to save a boolean issetBool.Step 2: Check method correctness
Options A, B, and D use incorrect method names that do not exist in SharedPreferences API.Final Answer:
prefs.setBool('isLoggedIn', true); -> Option DQuick Check:
Save boolean = setBool [OK]
- Using incorrect method names like saveBool or putBool
- Forgetting to await async calls
- Confusing setBool with setString
final prefs = await SharedPreferences.getInstance();
await prefs.setInt('counter', 5);
int count = prefs.getInt('counter') ?? 0;
print(count);Solution
Step 1: Analyze saving the value
The code saves integer 5 with key 'counter' usingsetInt.Step 2: Analyze reading the value
It reads the value withgetInt. Since 'counter' exists, it returns 5, not the default 0.Final Answer:
5 -> Option CQuick Check:
Saved int 5, getInt returns 5 [OK]
- Assuming getInt returns null instead of default
- Confusing setInt with setString
- Forgetting to await setInt
final prefs = SharedPreferences.getInstance();
prefs.setString('username', 'Alice');Solution
Step 1: Check getInstance usage
getInstance()returns a Future, so it must be awaited or handled asynchronously.Step 2: Check setString call
CallingsetStringon a Future object (not the SharedPreferences instance) causes error.Final Answer:
Missing await before getInstance() -> Option BQuick Check:
Always await SharedPreferences.getInstance() [OK]
- Not awaiting getInstance()
- Calling setString on Future instead of instance
- Confusing async calls with sync
Solution
Step 1: Check async usage
SharedPreferences.getInstance() must be awaited to get the instance before use.Step 2: Check method calls and types
setString saves the theme string correctly; setBool saves the boolean flag correctly. Both use await for async calls.Step 3: Identify errors in other options
final prefs = SharedPreferences.getInstance(); prefs.setString('theme', 'dark'); prefs.setBool('notifications', true); misses await on getInstance and set methods; final prefs = await SharedPreferences.getInstance(); prefs.setInt('theme', 'dark'); prefs.setBool('notifications', true); uses setInt('theme', 'dark') causing type error ('dark' not int); final prefs = await SharedPreferences.getInstance(); await prefs.setInt('theme', 1); await prefs.setBool('notifications', 'true'); uses wrong types (setInt for string, boolean as string).Final Answer:
final prefs = await SharedPreferences.getInstance(); await prefs.setString('theme', 'dark'); await prefs.setBool('notifications', true); -> Option AQuick Check:
Await getInstance and setX methods with correct types [OK]
- Not awaiting async calls
- Using wrong data types for values
- Saving booleans as strings
Ready to prove it?
Pass the test and this pattern moves unknown
