0%
Local database

Local storage

Keeping data after the app is closed

15-20 min

Local database

Everything we've built so far disappears when the app closes. The cart from lesson 7 lives in a ChangeNotifier, which means it lives in RAM: the operating system reclaims that memory as soon as it needs it, and the user comes back to an empty cart without understanding why. On mobile, this isn't a hypothetical: Android and iOS kill background apps all the time.

Local persistence serves two needs that you need to tell apart, because they call for different tools. The first is preferences: a boolean for dark mode, a language, a session token. A few dozen isolated values that you read at startup and rarely write. The second is business data: hundreds of rows that you want to filter, sort, join, and count. Storing preferences in a SQL database is needless complexity; storing a catalog of a thousand items in a preferences file is a mistake that costs you in startup time.

This lesson covers the two reference tools — shared_preferences and sqflite — with an explicit selection criterion, then the two real difficulties of SQLite on mobile: schema migration when the app is already installed, and transactions.

Comments

Comments are powered by GitHub Discussions

Sign in with GitHub to join the discussion

Link copied!