Sub topic 1
Where can a Flutter app keep data?
Prerequisites
This chapter assumes that you know basic Dart classes and null safety, can use Future, async, and await, and have added packages through pubspec.yaml. You should also understand that Flutter code can run on several platforms, each with its own storage rules.
A Flutter app usually stores data locally for one of four reasons:
- Remember a small setting, such as a theme choice.
- Keep structured application data, such as notes or tasks.
- Store larger serialized data, such as downloaded documents.
- Protect sensitive values, such as authentication tokens.
The best storage choice depends on the data's size, structure, sensitivity, and access pattern.
The main storage options
Key-value storage keeps small values under names called keys. The shared_preferences package supports values such as strings, numbers, booleans, and lists of strings. It works well for settings and small flags. It is not a good database for thousands of records or complex queries.
File storage writes bytes to a file in an app-controlled directory. Your app can serialize an object to JSON, write the JSON text, and decode it later. Files suit documents, caches, exports, and data that you usually read or write as a whole.
Secure storage is designed for secrets. The fluttersecurestorage package uses platform security facilities, such as the iOS Keychain and Android Keystore. Use it for tokens, credentials, and encryption keys. Do not treat ordinary preferences as a safe place for secrets.
A database stores records in a structure that supports queries. SQLite, accessed in Flutter through packages such as sqflite, is useful for many related records, filtering, sorting, updates, and transactions. A database requires more design than preferences or a single file.
| Storage type | Best for | Main limitation |
|---|---|---|
| Key-value preferences | Small settings and flags | Weak support for structured records |
| JSON files | Documents and whole-object data | You usually manage reading and rewriting yourself |
| Secure storage | Tokens and other secrets | Small capacity and limited querying |
| SQLite database | Structured collections and queries | Requires schema and migration work |
A decision path
Start by classifying the data rather than choosing a package by habit. A dark-mode setting is a boolean, so preferences are enough. A list of notes may begin as one JSON file, but SQLite becomes more suitable when users need searching, sorting, or partial updates. An access token is sensitive even if it is only one string, so it belongs in secure storage.
Directories and platform behavior
Files should not be written to an arbitrary path. The path_provider package finds directories that the operating system gives your app for documents, temporary files, and other purposes. Choose a directory according to the data's lifetime.
Documents directories are appropriate for user data that should remain available between launches. Temporary directories are appropriate for caches that the app can recreate. Operating systems may remove temporary files when storage is limited.
Local storage is private to the app in normal use, but it is not automatically a backup. Users can uninstall the app, clear its data, or lose the device. If data must survive those events, add a separate export or synchronization strategy.
A practical rule
Use the simplest option that meets the requirements:
- Choose secure storage for secrets.
- Choose preferences for a few small settings.
- Choose a file for data read and written as a complete document.
- Choose SQLite when records need reliable queries and independent updates.
Later chapters will implement each option and place the details behind a repository, so the rest of the app does not depend directly on a storage package.
Exercise
A note-taking app has a theme setting, an authentication token, and 20,000 searchable notes. Which storage option should each use?
Show answer
Store the theme setting in key-value preferences, the authentication token in secure storage, and the notes in SQLite because they are numerous and require queries.