When to use shared_preferences and its primary limitations?
This tests whether you know appropriate local persistence tiers. A strong answer names simple primitives like flags or tokens, notes async disk writes, and warns against large blobs, relational data, or secrets.
What's really being asked
Your judgment in selecting local persistence layers. The interviewer wants to see that you understand shared_preferences is a lightweight key-value store and that you can articulate exactly where it stops being useful. Senior engineers should map data characteristics to storage backends without defaulting to the easiest option.
The full answer
Four things in order. First, ideal use cases: small primitive values like feature flags, onboarding completion booleans, user preference strings, or lightweight authentication tokens. Second, the API model: asynchronous get and set operations backed by an in-memory cache that flushes to disk, which means you must await writes and handle null defaults. Third, concrete limitations: no querying or indexing, no support for nested objects without manual serialization, poor performance for large payloads or high-frequency writes because it rewrites the entire file, and no built-in encryption so it is unsafe for passwords or PII. Fourth, alternatives: SQLite or Hive for relational or structured data, path_provider with dart:io for large files, and flutter_secure_storage for secrets.
The mistakes people make
Three red flags. One, calling it synchronous. Two, suggesting it for caching network images or large JSON blobs without mentioning size constraints. Three, claiming it is secure or encrypted by default. Another subtle trap is forgetting to mention that it stores data as simple key-value pairs and therefore cannot handle complex data models without extra work.
What usually comes next
The interviewer may ask how you would store a complex object, which should lead to jsonEncode and jsonDecode with a String value. They might ask about testing, where you should mention SharedPreferences.setMockInitialValues. They could also ask about concurrent writes across isolates or platform-specific backing stores, which on Android uses SharedPreferences and on iOS uses NSUserDefaults.
A concrete example
Suppose you need to persist whether a user has seen a tutorial. A boolean flag named hasSeenTutorial is perfect for shared_preferences because it is tiny, read once at startup, and updated once. If you later need to store the user's entire offline message history with search, shared_preferences would be the wrong tool because you would load a massive string into memory on every read and rewrite the entire file on every new message. You would migrate to SQLite with proper indexing.
Interview question
When should you choose shared_preferences over SQLite or secure storage?
- a.When saving a user's password for automatic login
- b.When storing a relational product catalog that requires complex search queries
- c.When persisting a 5 MB JSON configuration file that changes every minute
- d.When caching a boolean onboarding flag that is read once at startupCorrect
Why? this is the answer
A boolean onboarding flag is small, primitive, and read infrequently, which aligns perfectly with shared_preferences as a lightweight key-value store. Option C is tempting because JSON seems like simple data, but a large, frequently updated payload performs poorly since shared_preferences rewrites the entire file on every change.
Just read this? Test yourself on what you have been reading.
Read the original → docs.flutter.dev
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on flutter — each one lists the topics its interview covers.
See open roles