Using SharedPreferences for key-value storage is lightweight and fast for small data. It runs on the main thread but is optimized for quick reads and writes, so it usually does not affect frame rate. However, frequent writes can cause minor delays and battery use if done excessively.
SharedPreferences for key-value in Flutter - Build, Publish & Deploy
To keep your app smooth at 60fps, avoid writing to SharedPreferences too often. Batch updates or write only when necessary. Read values once and cache them in memory if used frequently. Use async calls properly to prevent blocking the UI thread.
SharedPreferences adds minimal size to your app bundle since it is part of Flutter plugins. It does not significantly affect startup time because it loads data asynchronously. Keep stored data small to avoid slow reads during app launch.
On Android, SharedPreferences uses XML files stored in app storage. On iOS, it uses NSUserDefaults. Both platforms handle small key-value pairs efficiently. iOS may cache values more aggressively, so changes might not appear instantly if not synced properly.
Ensure your app respects user privacy when storing data. Do not store sensitive information like passwords in SharedPreferences without encryption. Follow Apple's and Google's guidelines on data storage and user consent for personal data.
Your app takes 5 seconds to load this screen. What's likely wrong?
- Excessive synchronous reads or writes to SharedPreferences blocking the UI thread.
- Storing large amounts of data in SharedPreferences causing slow access.
- Not caching values in memory, causing repeated slow reads.
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
