Dart Code Generation with build_runner
Set up Dart and Flutter source generation with build_runner and Freezed. Use for missing generated parts, JSON or immutable models, conflicting outputs and reproducible CI.
Choose one existing model and identify which files own input and output. For a Freezed model, distinguish the annotations used by source code from the generator used at build time. Add the compatible annotations to ordinary dependencies and the generator and build_runner to dev dependencies as appropriate for the selected package. If JSON generation is required, configure the compatible JSON annotations and generator separately. Resolve versions against the Dart SDK and the repository lockfile policy. ## Key Points - [Dart build_runner documentation](https://dart.dev/tools/build_runner) - [Freezed maintainer documentation](https://pub.dev/packages/freezed)
skilldb get flutter-skills/dart-code-generationFull skill: 28 linesDart Code Generation with build_runner
Choose one existing model and identify which files own input and output. For a Freezed model, distinguish the annotations used by source code from the generator used at build time. Add the compatible annotations to ordinary dependencies and the generator and build_runner to dev dependencies as appropriate for the selected package. If JSON generation is required, configure the compatible JSON annotations and generator separately. Resolve versions against the Dart SDK and the repository lockfile policy.
Align the library, parts and builders
Follow the chosen generator's syntax for the exact pinned version. Ensure the original library declares the expected part file names and generator inputs, including a JSON factory when the model needs deserialization. Match actual file base names and use the correct import and part relationship. Check build.yaml builder enablement and target globs if generation misses one directory. Avoid relying on files accidentally generated by another package or workspace member.
From the package or workspace context documented for the project, run dart run build_runner build and inspect emitted files, diagnostics and exit status. Use watch for local iteration when it helps, and keep one owner for a given generated output at a time. Do not edit generated implementation directly. For conflicting outputs, first establish whether the existing file is a generated derivative or a hand-authored input. Review the affected paths before using any flag that deletes conflicting outputs.
Validate behavior and reproducibility
Exercise a new model, a field rename and optional/null/default values. Test a JSON round trip when relevant, including compatible older input and any contractually rejected input. Verify immutability and equality only where the generated API actually promises those properties. Run the project's analyzer and tests on the freshly generated code rather than using stale checked-in outputs to make an import succeed.
Choose whether generated source is committed or rebuilt in CI based on repository packaging rules. If committed, regenerate under the pinned toolchain in CI and fail on unexpected diff. If regenerated, verify a clean checkout builds without any pre-existing generated part. Keep independent build and watch checks: a watch update pass does not establish first-run dependency configuration. Retain command, versions and diagnostics for any difference.
References and rights
Original SkillDB instructions under LicenseRef-SkillDB-Content. References were checked 2026-11-13 and used for technical verification.
Install this skill directly: skilldb add flutter-skills
Related Skills
Flutter Semantics and Accessibility Testing
Verify Flutter widget semantics, focus, reading order and gestures with accessibility tests and screen readers. Use for custom widgets, regressions or screen-reader interaction failures.
Profile Flutter Startup and Frame Performance
Measure Flutter mobile startup and rendering on physical devices in profile mode, distinguish UI and raster work, and verify fixes against comparable traces.
Local Storage
Hive, SharedPreferences, and Drift patterns for local data persistence in Flutter
Manage Flutter and Dart Monorepos with Melos
Set up Dart Pub workspaces and Melos for local package resolution, scoped scripts, and predictable verification across dependent Flutter packages.
Navigation
GoRouter navigation patterns for declarative, deep-linkable Flutter routing
Networking
Dio HTTP client and API integration patterns for Flutter applications