Sub topic 1
SQLite on device and where drift fits
Prerequisites
This chapter assumes you know basic Dart and Flutter, including widgets and StatefulWidget. You should be comfortable with async and await, and you should know what a table, row, and primary key are. You also need the Dart and Flutter command-line tools on your path.
SQLite on device and where drift fits
What SQLite gives you
SQLite is a small database engine that stores all its data in one file on the device. It needs no server and no network. Flutter apps on mobile, desktop, and web can use it to keep data between launches.
Writing raw SQL strings in Dart is error-prone. Typos reach runtime, and query results arrive as untyped maps. drift is a library that sits on top of SQLite. You describe tables in Dart, and drift generates typed query methods. A misspelled column becomes a compile error.
Installing drift and the code generator
Add the runtime packages and the code generator with these commands:
flutter pub add drift drift_flutter
flutter pub add dev:drift_dev dev:build_runnerdrift_flutter opens the database file in the right place on each platform. drift_dev and build_runner are development-only tools that write the generated code.
Describing your schema as Dart classes
Each table is a Dart class that extends Table. Each getter defines one column:
import 'package:drift/drift.dart';
class TodoItems extends Table {
IntColumn get id => integer().autoIncrement()();
TextColumn get title => text().withLength(min: 1, max: 100)();
BoolColumn get isDone => boolean().withDefault(const Constant(false))();
}The id column is the primary key. withLength and withDefault add constraints that SQLite enforces.
Generating code with build_runner
Run the generator once after you write or change a table:
dart run build_runner buildIt reads your annotated classes and writes a file named database.g.dart, next to your source file. That generated file holds the code that your database class inherits. Do not edit it by hand, because the generator overwrites it.
Creating the database class and opening it
Put the tables and the database class in one file, database.dart:
import 'package:drift/drift.dart';
import 'package:drift_flutter/drift_flutter.dart';
part 'database.g.dart';
class TodoItems extends Table {
IntColumn get id => integer().autoIncrement()();
TextColumn get title => text().withLength(min: 1, max: 100)();
BoolColumn get isDone => boolean().withDefault(const Constant(false))();
}
@DriftDatabase(tables: [TodoItems])
class AppDatabase extends _$AppDatabase {
AppDatabase([QueryExecutor? executor])
: super(executor ?? driftDatabase(name: 'app'));
@override
int get schemaVersion => 1;
}part 'database.g.dart'; links the generated code into this file. _$AppDatabase is the generated base class. driftDatabase(name: 'app') opens the SQLite file on the device. The optional executor parameter lets tests pass a different connection.
> Note. The schemaVersion value starts at 1. You raise it later, when you change the schema and write a migration.
Exercise
Exercise
You add a new column to TodoItems and run your app without running build_runner again. What goes wrong, and why?
Show answer
The generated code in database.g.dart still describes the old columns. Queries and row classes do not include the new column, so the app cannot use it. Run dart run build_runner build after every table change to regenerate the code.