Data Persistence Concepts
Learn about persisting data locally in your Flutter app.
We'll cover the following
Data persistence in Flutter
Most mobile apps require that we handle data. It would be frustrating to lose the data whenever it restarts. Saving app data on non-volatile storage to make it available even when the app is fully closed and restarted is called data persistence.
The following are the advantages of data persistence:
- Persistent data is preserved even when the user closes the app, allowing users to resume from where they left off. Continuous data allows users to work when they lose internet connectivity or get offline.
- Only the user can delete continuous data; this assures later data access.
Flutter offers several data-persistence options for us to choose from. These include:
- Key-value store
- File storage
- Local database
- Remote data store
Key-value store
Here, we store data as a collection of key-value pairs. A key is a unique identifier to the corresponding value stored. Both iOS and Android have native solutions to store data as key-value pairs on disk: NSUserDefaults on iOS and SharedPreferences on Android. Flutter offers the plugin shared_preferences
, which wraps both native solutions.
Primitive data types are stored using the shared_preferences
plugin. These include:
- Numbers
- Strings
- Boolean
We use the shared_preferences
plugin to store small app features, such as:
- User progress, for example, advancement in games
- User preference, for example, dark mode and light mode
- User credentials, for example, authentication tokens to hide some data depending on whether a user has logged in
We shouldn’t use the shared_preferences
plugin when:
- Storing complex data
- Storing sensitive data, such as passwords
File storage
Sometimes, we might need to read and write files to disk. Flutter offers a disk-based file system that we can utilize using the File
class.
Each app has a named internal storage directory to store private files that are only accessible to the specific app. The system deletes the saved files when the app is uninstalled.
We can use file storage to store:
- Documents
- Encrypted information
- Media content
Local database
Storing a large amount of structured data requires using a database instead of file storage or a key-value store. A database is a reliable and easily accessible collection of organized data.
Both Android and iOS provide access to the SQLite database management system (DBMS) based on SQL. Some SQLite features include the following:
- It uses a single file to store tables.
- It’s small and self-contained—no external dependencies are required.
- It does not require any setup.
- It allows access to multiple database files simultaneously in a single connection.
- It requires some SQL knowledge to work with.
Flutter can use SQLite DBMS via the sqflite
plugin or the Moor
package.
Remote database
The following two problems can occur when using the local database, file storage, and key-value store:
- App data is lost when a user uninstalls the app.
- Users cannot access the stored data when they log in on another device.
To overcome this, we’ll need to store data on a remote database such as Firebase’s Realtime Database or Cloud Firestore.