Choosing a Flutter Local Persistence Strategy
Flutter offers three main ways to persist data locally. Your options are simple key-value pairs for settings, direct file I/O for blobs like images, or a full SQLite database for complex, structured data. The main footgun is picking the wrong tool for the job.
Why it exists
Apps often need to store data beyond the app's lifecycle, such as user preferences, cached information, or content for offline use. Flutter provides several approaches to save data directly on the user's device, each solving a different kind of storage problem.
The mental model
Think of local persistence as choosing the right container for your data. For a single setting, you use a small labeled box (key-value store). For a large, unstructured object like a photo, you use a generic shipping crate (a file). For a whole collection of organized, related items you need to search and filter, you use a filing cabinet (SQLite database).
How it works
Flutter's official documentation highlights three primary paths for local persistence. First, storing key-value data on disk, which is ideal for simple data like flags, tokens, or user settings. Second, reading and writing files, giving you direct access to the filesystem for storing unstructured data like JSON blobs, images, or logs. Third, persisting data with SQLite, a powerful embedded database for handling large amounts of structured, relational data that needs to be queried efficiently.
When to use it
Use key-value stores (like shared_preferences) for small, simple pieces of data. Use file storage (like path_provider) when dealing with larger, self-contained data blobs that you don't need to query internally, such as a downloaded PDF or a saved user drawing. Use SQLite (via the sqflite package) when your app manages complex, structured data that requires searching, filtering, or relational links, like a to-do list app or a contact manager.
When not to use it
Avoid using a complex tool for a simple job, or a simple tool for a complex one. Don't use SQLite just to store a single boolean for dark mode. Don't use a key-value store to manage thousands of relational records; you'll end up building a slow and buggy database yourself. Don't use raw files if you need to perform complex queries on the data within them.
One canonical example
An app that needs to remember a user's dark mode preference would use a key-value store. An app that saves a user-generated photo would write it to a file. An e-commerce app that manages a local cache of products with different categories, prices, and inventory levels would use SQLite to allow for efficient filtering and searching.
Interview question
For an app managing a collection of user-created notes, each with a title, content, and creation date, requiring efficient search and filtering, which local persistence strategy is best?
- a.Utilizing a SQLite database for structured dataCorrect
- b.Saving all notes together as a single JSON file
- c.Using a key-value store for each note's properties
- d.Storing each note as a separate file on the device
Why? this is the answer
SQLite is the most appropriate choice for managing structured, relational data that requires efficient querying, filtering, and searching, as described for a collection of notes. Key-value stores are for simple settings, and file I/O (whether individual files or a single JSON blob) is less efficient for complex queries across multiple structured records compared to a database.
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