Flutter offers multiple ways to store data persistently — depending on the type, size, and frequency of data access. Here are the most commonly used solutions:
1. SQFlite – SQLite Database for Flutter
Use Case:
Ideal for storing structured, relational data that requires queries, indexing, or transactions.
Details:
The sqflite package provides a simple and efficient way to work with an SQLite database directly from Flutter.
SQLite is a lightweight relational database that stores data locally on the device.
Steps:
- Use the
pathpackage to locate or create the database file in the appropriate directory supported by the platform (Android, iOS, Web, etc.). - Then, use
sqfliteto open the database, define tables, and perform CRUD operations.
Reference:
🔗 SQLite Tutorial
2. XML-based Key-Value Store – shared_preferences
Use Case:
Best suited for lightweight, primitive data (e.g., user settings, app theme, login status, tokens).
Details:
The shared_preferences plugin provides persistent storage for simple data types such as strings, integers, and booleans.
Internally, it stores data in an XML-based key-value format on Android and in UserDefaults on iOS.
Example Use Cases:
- Saving user preferences
- Remembering “dark mode” settings
- Caching login session tokens
3. File Storage
Use Case:
Used for storing files, logs, serialized data, or large custom data objects.
Packages Used:
path_provider→ To locate system directoriesdart:io→ To perform file read/write operations
Common Directories:
- Cache Directory
- Use
getCacheDir() - Temporary directory at the device level.
- Data here can be cleared automatically by the system when storage is low.
- Use
- Application’s Private Directory
- Use
getApplicationDocumentsDirectory() - Private to the app and not accessible by other apps.
- Automatically deleted when the app is uninstalled.
- Use
✅ Summary:
| Storage Type | Package | Data Type | Persistence | Ideal Use |
|---|---|---|---|---|
| SQFlite | sqflite | Structured (tables) | Long-term | Complex relational data |
| Shared Preferences | shared_preferences | Key-Value (primitive) | Long-term | Settings, flags |
| File Storage | path_provider, dart:io | Files & Objects | Long-term | Custom or binary data |
