SharedPreferences is a way to store small amounts of data as key-value pairs on the device. It is used for saving simple data like user settings or preferences.
Click to reveal answer
beginner
How do you save a string value using SharedPreferences in Flutter?
You get an instance of SharedPreferences and then use the method setString(key, value) to save a string value with a key.
Click to reveal answer
beginner
Which method do you use to read a value from SharedPreferences?
Use getString(key) for strings, getInt(key) for integers, and similar methods for other types to read stored values by their key.
Click to reveal answer
intermediate
Can SharedPreferences store complex objects like lists or maps directly?
No, SharedPreferences only stores simple data types like strings, ints, doubles, bools, and string lists. For complex objects, you need to convert them to a string format like JSON.
Click to reveal answer
beginner
Why is SharedPreferences suitable for storing user settings?
Because it is fast, simple, and persists data across app launches without needing a database. It is perfect for small pieces of data like user preferences.
Click to reveal answer
Which method is used to save a boolean value in SharedPreferences?
AstoreBool(key, value)
BsaveBoolean(key, value)
CputBool(key, value)
DsetBool(key, value)
✗ Incorrect
The correct method to save a boolean value is setBool(key, value).
What type of data can SharedPreferences NOT store directly?
ACustom objects
Bint
CString
DList of strings
✗ Incorrect
SharedPreferences cannot store custom objects directly; they must be converted to strings first.
How do you get an instance of SharedPreferences in Flutter?
ASharedPreferences.instance()
BSharedPreferences.getInstance()
CSharedPreferences.create()
DSharedPreferences.new()
✗ Incorrect
You use SharedPreferences.getInstance() which returns a Future of SharedPreferences.
Which of these is a correct way to read a string value from SharedPreferences?
Aprefs.getString('key')
Bprefs.readString('key')
Cprefs.fetchString('key')
Dprefs.loadString('key')
✗ Incorrect
The method to read a string is getString(key), so prefs.getString('key') is correct.
What happens if you try to read a key that does not exist in SharedPreferences?
AReturns an empty string
BThrows an error
CReturns null
DReturns zero
✗ Incorrect
If the key does not exist, SharedPreferences returns null.
Explain how to save and retrieve a user preference using SharedPreferences in Flutter.
Think about how you get the SharedPreferences object and then use set and get methods.
You got /5 concepts.
Describe the limitations of SharedPreferences and when you might need a different storage solution.
Consider what kind of data SharedPreferences can handle well and what it cannot.
You got /5 concepts.
Practice
(1/5)
1. What is the main purpose of using SharedPreferences in a Flutter app?
easy
A. To save small key-value data that persists after the app closes
B. To create complex database tables
C. To design the app's user interface
D. To handle network requests
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 A
Quick Check:
SharedPreferences = persistent small key-value storage [OK]
Hint: Remember: SharedPreferences stores simple data persistently [OK]
Common Mistakes:
Confusing SharedPreferences with databases
Thinking it manages UI or network
Assuming it stores large files
2. Which of the following is the correct way to save a boolean value true with key isLoggedIn using SharedPreferences in Flutter?
easy
A. prefs.putBool('isLoggedIn', true);
B. prefs.saveBool('isLoggedIn', true);
C. prefs.storeBool('isLoggedIn', true);
D. prefs.setBool('isLoggedIn', true);
Solution
Step 1: Recall SharedPreferences method names
The correct method to save a boolean is setBool.
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 D
Quick Check:
Save boolean = setBool [OK]
Hint: Use setBool to save booleans in SharedPreferences [OK]
Common Mistakes:
Using incorrect method names like saveBool or putBool
Forgetting to await async calls
Confusing setBool with setString
3. Given the following Flutter code snippet, what will be printed?
final prefs = await SharedPreferences.getInstance();
await prefs.setInt('counter', 5);
int count = prefs.getInt('counter') ?? 0;
print(count);
medium
A. 0
B. null
C. 5
D. Error
Solution
Step 1: Analyze saving the value
The code saves integer 5 with key 'counter' using setInt.
Step 2: Analyze reading the value
It reads the value with getInt. Since 'counter' exists, it returns 5, not the default 0.
Final Answer:
5 -> Option C
Quick Check:
Saved int 5, getInt returns 5 [OK]
Hint: getInt returns saved value or default if missing [OK]
Common Mistakes:
Assuming getInt returns null instead of default
Confusing setInt with setString
Forgetting to await setInt
4. What is wrong with this Flutter code that tries to save a string in SharedPreferences?
final prefs = SharedPreferences.getInstance();
prefs.setString('username', 'Alice');
medium
A. setString method does not exist
B. Missing await before getInstance()
C. Key 'username' cannot be a string
D. Cannot save strings in SharedPreferences
Solution
Step 1: Check getInstance usage
getInstance() returns a Future, so it must be awaited or handled asynchronously.
Step 2: Check setString call
Calling setString on a Future object (not the SharedPreferences instance) causes error.
Final Answer:
Missing await before getInstance() -> Option B
Quick Check:
Always await SharedPreferences.getInstance() [OK]
Hint: Always await getInstance() before using prefs [OK]
Common Mistakes:
Not awaiting getInstance()
Calling setString on Future instead of instance
Confusing async calls with sync
5. You want to save a user's theme preference as a string ('light' or 'dark') and a flag if notifications are enabled (boolean). Which of the following code snippets correctly saves both values using SharedPreferences in Flutter?
hard
A. final prefs = await SharedPreferences.getInstance();
await prefs.setString('theme', 'dark');
await prefs.setBool('notifications', true);
B. final prefs = SharedPreferences.getInstance();
prefs.setString('theme', 'dark');
prefs.setBool('notifications', true);
C. final prefs = await SharedPreferences.getInstance();
prefs.setInt('theme', 'dark');
prefs.setBool('notifications', true);
D. final prefs = await SharedPreferences.getInstance();
await prefs.setInt('theme', 1);
await prefs.setBool('notifications', 'true');
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 A
Quick Check:
Await getInstance and setX methods with correct types [OK]
Hint: Await all async SharedPreferences calls with correct types [OK]
Common Mistakes:
Not awaiting async calls
Using wrong data types for values
Saving booleans as strings
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.