Local storage and caching
The right local storage depends on the nature of the data:
- `shared_preferences` — small key-value pairs: theme choice, whether onboarding was seen, the last selected filter. It is not read synchronously; it loads once at startup
- SQLite (`sqflite`) or another database — structured data you need to query: order history, an offline cache
- The file system — images, documents, exported files
- Secure storage — tokens and passwords must not live in `shared_preferences`; that needs a package backed by the platform keychain/keystore
Caching strategy depends on the data too:
- Cache-first — show local data, then refresh in the background. Fast startup, at the cost of briefly showing stale data
- Network-first — try the network, fall back to local. For data where freshness matters: a bank balance, an order status
- Cache-only — offline mode
The part most often forgotten is invalidation: deciding when cached data has gone stale. The approaches are time-based (TTL), event-based (the user made a change) or version-based.
Interview tip. A security question hides here: shared_preferences is not encrypted — on a rooted Android device or from an iOS backup it is readable. Tokens, passwords and personal data need the platform's secure storage. Raising that yourself demonstrates security awareness, which counts for a lot in banking and insurance work.
📚 Sources and documentation
- Store key-value dataofficialdocs.flutter.dev
- Persist data with SQLiteofficialdocs.flutter.dev
- shared_preferences packageofficialpub.dev
- sqflite packagepub.dev