Sub topic 2
Installing drift and the Code Generator
Prerequisites
This chapter assumes you can create and run a Flutter project, use Dart async and await, and recognize basic database terms such as table and row. You do not need to write SQL yet. The goal here is to install drift and confirm that its code-generation tool is available.
[drift](https://pub.dev/packages/drift) is a Dart library that provides typed access to SQLite. SQLite is the database engine that stores data in a local file on the device. Drift generates Dart code from your table definitions, which catches many mistakes at compile time.
Add the packages
Run these commands from your Flutter project's root directory:
flutter pub add drift drift_flutter
flutter pub add dev:drift_dev dev:build_runnerThe first command adds packages needed when the application runs. drift provides the database API. drift_flutter provides Flutter-friendly database opening and platform support.
The second command adds development-only packages. drift_dev contains the generator. build_runner runs generators and writes their output. Development-only packages are not imported by the application at runtime.
Your dependency groups should have roles like these:
| Package | Dependency group | Purpose |
|---|---|---|
| `drift` | `dependencies` | Typed database API used by app code |
| `drift_dev` | `dev_dependencies` | Generates Dart database code |
| `build_runner` | `dev_dependencies` | Runs the generator |
| `drift_flutter` | `dependencies` | Opens a Flutter-compatible SQLite database |
flutter pub add updates pubspec.yaml and resolves compatible versions. Commit both pubspec.yaml and pubspec.lock when your project convention requires the lockfile.
Run code generation
After you add a database source file containing drift annotations, run:
dart run build_runner buildThe command scans Dart files, creates generated files, and exits. A generated file commonly ends in .g.dart. Do not edit that file manually. Change the source file instead, then run the generator again.
During development, use watch mode to regenerate after each source change:
dart run build_runner watchStop watch mode with Ctrl+C. Use build in continuous integration because it performs one predictable generation step.
Common generation errors
- The command cannot find
build_runner: Run it from the directory containingpubspec.yaml, then runflutter pub get. - A generated file is missing: Confirm that the source file has the required
partdirective and that the file name matches the generated output name. - Conflicting outputs appear: Stop other generator processes and run
dart run build_runner build --delete-conflicting-outputs. Use this option only when replacing stale generated files is acceptable. - The generator reports an analyzer error: Fix the Dart error first. Generation depends on valid source code.
Exercise
Which command should you use to regenerate drift files once, and which command should you use while actively editing database code?
Show answer
Use dart run build_runner build for one generation run and dart run build_runner watch for continuous regeneration during development.