Persisting data locally in
Flutter
D A N V I C K M I L L E R
Slide 2
Slide 2 text
Why
• Storing user data, preferences & tokens
• Caching remote requests
• Building offline (or offline-first) apps
Example: Password Managers or apps used in remote regions
with intermittent internet
Slide 3
Slide 3 text
How to
• Key-value stores
• Local Files
• SQL Databases
• NoSQL Databases
Slide 4
Slide 4 text
Key-value stores
• Easily save basic data as key-value pairs in a private persisted dictionary.
• Used for app preferences, keys and session information.
• Example: Shared Preferences which uses NSUserDefaults on iOS and
macOS, SharedPreferences on Android, etc.
Slide 5
Slide 5 text
Local files
• Save arbitrary files to internal or external device storage.
• Flutter can read/write files to internal as well as external storage.
• Applications have access to an application-specific directory where files
can be stored.
• Often used for blob data or data file caches (i.e. disk image cache)
Slide 6
Slide 6 text
SQL Databases
• Persist relational data in tables within an application specific database
• Used for complex local data manipulation or for raw speed
• SQLite is the most common SQL database. Usually used with ORMs to
make data manipulation easy.
• SQLite database file is stored in your application-specific directory.
• Examples of ORMs: Moor
Slide 7
Slide 7 text
NoSQL Databases
• Persist data in collections and documents
• Stores unstructured, semi-structured, or structured data
• Can be developer-friendly - Enable easy updates to schemas and fields
• Examples: Hive, Sembast, ObjectDB
Slide 8
Slide 8 text
Notes:
• All the persistance options may be interchangable; choose optimal
solution
• Data stored persisted can be deleted by the user; will also be deleted
once the app is uninstalled
• When pushing an update always handle migration of data structures
(tables and collections) when there are changes