Sub topic 14
Choosing the Right Local Storage Option in Flutter
This chapter assumes you know basic Dart classes, null safety, Future, async and await, Flutter state, and how to add packages in pubspec.yaml. The goal is to choose storage based on the data rather than choosing a package first.
Start with the data
Ask these questions:
- How large can the data become?
- Is the data one value, a document, or many related records?
- Must the user search, sort, filter, or join records?
- Is the data secret?
- Can the data be recreated from a server?
- Will the data structure change in future app versions?
The answers usually point to a storage type.
| Need | Good default | Reason |
|---|---|---|
| A few settings such as theme or first-run state | `shared_preferences` | Simple key-value access |
| A complete JSON document or export | A file in an app directory | Natural document representation |
| Tokens, passwords, or encryption keys | `flutter_secure_storage` | Uses platform secure storage |
| Many records with filters and relationships | SQLite with `sqflite` or Drift | Supports structured queries |
| Flexible objects without SQL | An embedded NoSQL or object store | Stores object-shaped data |
| Temporary data that can be downloaded again | Memory or a cache file | Avoids unnecessary permanent storage |
A storage choice describes both the data and its operations. A list of notes may fit in a JSON file at first. If users later need title searches, sorting, partial updates, and thousands of records, SQLite becomes a better fit.
Compare the main trade-offs
shared_preferences is convenient for primitive values such as strings, numbers, and booleans. It is not a general database. Do not store a growing list of objects by repeatedly encoding the entire list as one preference value. That approach makes updates inefficient and can create size and consistency problems.
Files work well when the app reads or writes a whole document. JSON is easy to inspect and exchange, but your code must serialize models and handle malformed or older files. Use path_provider to find an app-owned directory instead of constructing platform paths yourself.
Secure storage is for confidentiality, not for large application data. Store a token there, then keep ordinary profile or cache data elsewhere. A secure container does not make every value in your app secure. Avoid logging secrets and delete them on sign-out when appropriate.
SQLite is appropriate when data has rows, columns, indexes, and query requirements. Use parameter placeholders such as where: 'title = ?' and pass values through whereArgs. Never build SQL by concatenating user input. When the schema changes, increase the database version and write an onUpgrade migration.
Drift adds generated, type-safe database access around SQLite. It can reduce errors in larger applications, but it introduces generated code and another layer to learn. A small app may use sqflite directly; a growing app with many queries and models may benefit from Drift.
Use a decision process
Hide the choice behind a repository
A repository is a class that gives the rest of the app operations such as loadNotes and saveNote without exposing package APIs. This separation lets the UI depend on a stable interface. You can replace a JSON file with SQLite later, or use an in-memory fake in tests, without rewriting widgets.
Keep serialization and storage errors at the repository boundary. Decide whether a missing value means “use a default,” “return an empty collection,” or “show an error.” Document that decision so callers behave consistently.
Exercise
You need to save a theme boolean, an authentication token, and 50,000 searchable notes. Which storage should each use?
Show answer
Store the theme boolean in sharedpreferences, the token in fluttersecure_storage, and the notes in SQLite, using parameterized queries and a repository.