Sub topic 1
SQLite on Device and Where drift Fits
SQLite is a small relational database engine that runs inside your Flutter application. It stores data in a local file on the device. Because the file remains after the app closes, SQLite is useful for data such as notes, tasks, cached records, and offline changes.
A relational database stores information in tables. Each table contains rows, and each row contains values in named columns. A primary key identifies one row. For example, a todo_items table might contain an id, title, is_done, and created_at column.
Prerequisites
This chapter assumes that you know basic Dart and Flutter, including widgets, async and await, Future, and Stream. You should also understand that a relational database contains tables, rows, columns, and primary keys. The next chapters use the Flutter and Dart command-line tools.
Why use SQLite in a Flutter app?
An in-memory Dart list is easy to use, but its contents disappear when the process ends. A remote database preserves data centrally, but it requires a network connection and a server. SQLite gives the app durable local storage without requiring a server.
SQLite supports operations that become important as data grows:
- Selecting only rows that match a condition.
- Sorting and limiting results.
- Updating or deleting specific rows.
- Enforcing constraints such as required values and unique keys.
- Changing several related rows in one transaction.
The database file belongs to the app. Other applications normally cannot read it directly. You still need to consider sensitive data carefully because local storage is not automatically encrypted.
Where drift fits
Drift is a Dart persistence library built on SQLite. It provides a typed Dart interface for defining tables and writing queries. Drift also generates supporting Dart code from your table definitions.
The relationship looks like this:
Drift does not replace SQLite. SQLite performs the storage and query work. Drift adds Dart types, generated row classes, query helpers, and streams that notify the app when query results change.
| Layer | Main responsibility |
|---|---|
| Flutter UI | Displays data and accepts user actions |
| Application code | Decides when to read or write |
| drift | Describes the schema and builds typed queries |
| SQLite | Stores rows and executes queries |
| Device file system | Persists the database file |
A raw SQL approach can be flexible, but SQL strings can contain spelling or type errors that appear only at runtime. Drift moves many of those errors to code generation or compile time. Its generated code also keeps database rows aligned with Dart types.
The working model
You describe a table as a Dart class. Drift generates a row type for values read from that table and companion objects for values being written. Your database class opens the SQLite file and exposes queries. Flutter code then calls those queries or listens to their streams.
Exercise
Why might a Flutter app use SQLite through drift instead of storing records in a Dart list?
Show answer
A Dart list is lost when the process ends, while SQLite persists structured records on the device. Drift adds typed queries and generated code around SQLite.