Sub topic 4
Generating code with build_runner
This chapter assumes you know basic Dart, Flutter, SQLite tables, and drift table definitions. It also assumes that drift and drift_flutter are already familiar. The goal is to connect your Dart schema to the generated Dart code that drift uses at runtime.
What code generation does
Drift uses Dart classes to describe tables, columns, queries, and database connections. Some of the supporting code would be repetitive and error-prone to write by hand. A code generator creates it from your schema.
The build_runner package runs generators and writes their output into generated files. For drift, the generated file commonly contains:
- Row classes such as
TodoItem. - Companion classes such as
TodoItemsCompanion. - Table accessors such as
todoItems. - The database implementation used by your class.
- Type information that helps the Dart analyzer catch mistakes.
The generated code is derived from your source files. Do not edit it manually. Change the schema or database class, then run the generator again.
Add the generator packages
Run these commands from the Flutter project directory:
flutter pub add drift drift_flutter
flutter pub add dev:drift_dev dev:build_runner
dart run build_runner buildThe first command adds the runtime packages. The second adds development-only packages. The final command performs a one-time build.
Your pubspec.yaml will contain runtime dependencies for the application and development dependencies for code generation. Keeping generators in dev_dependencies prevents them from becoming part of the released application.
Connect a source file to its generated part
A Dart library includes its generated code with a part directive. The filename must match the file that the generator creates.
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() : super(driftDatabase(name: 'app'));
@override
int get schemaVersion => 1;
}The _$AppDatabase superclass does not exist in your handwritten source. Drift creates it in database.g.dart. The generated superclass connects your AppDatabase class to the declared tables and database executor.
The dependency chain looks like this:
Build modes and common errors
Use build for a single generation pass. During development, watch keeps the generator running and rebuilds after source changes:
dart run build_runner watchStop the watcher with Ctrl+C. Run a fresh build in continuous integration so generated output is checked in or produced before analysis and tests.
| Symptom | Likely cause | Fix |
|---|---|---|
| `Target of URI doesn't exist` | The generated part is missing | Run `dart run build_runner build` |
| Conflicting outputs are reported | Old generated files disagree with new outputs | Run with `--delete-conflicting-outputs` |
| `_$AppDatabase` is undefined | The `part` filename or database annotation is wrong | Check both and regenerate |
| A schema change has no effect | The generator did not run after editing | Run a build or use `watch` |
When a stale generated file causes a conflict, this command lets the generator replace only conflicting generated outputs:
dart run build_runner build --delete-conflicting-outputsUse this option for generated files only. Do not use it as a way to overwrite handwritten source.
Exercise
Why does part 'database.g.dart'; need to appear in the same Dart library as AppDatabase?
Show answer
The generated file defines members such as _$AppDatabase, and the part directive makes those generated declarations available to the library containing AppDatabase.