Sub topic 12
Keeping Generated Code in Sync
Assume you know basic Dart and Flutter project structure, can read simple Kotlin or Swift, and understand async and Future. This chapter focuses on the workflow after you define a Pigeon schema.
Pigeon generates source files from one input file. For example, pigeons/messages.dart can produce Dart, Kotlin, and Swift files. Those files are part of the build, but they are outputs, not the place where you design the API.
Treat the schema as the source of truth
Keep the Pigeon input file in version control. In most projects, also keep the generated files in version control. Checked-in generated files make the repository build immediately after checkout and let reviewers see the complete API change.
Do not edit generated files by hand. Your changes will disappear the next time someone runs Pigeon. Change the schema or the generator configuration instead, then regenerate every output.
A useful project layout separates the two kinds of files:
pigeons/
messages.dart # hand-written schema
lib/src/
messages.g.dart # generated Dart
android/app/src/main/kotlin/
com/example/app/Messages.g.kt
ios/Runner/
Messages.g.swiftThe generated files should have a clear comment or filename indicating that they are generated. The exact comment depends on the Pigeon version and configuration.
Pin the generator version
The schema and the Pigeon version together define the generated API. If two developers use different versions, they may produce different method signatures, serialization code, or native types.
Declare Pigeon as a development dependency and commit the lockfile when your project uses one:
dev_dependencies:
pigeon: 22.6.0Use the version selected by your project rather than copying this example blindly. Upgrade it deliberately, regenerate all outputs, run the tests, and review the generated diff.
The lockfile also matters because a version range can resolve to a newer package on a later install. A reproducible build uses the same resolved dependency versions in local development and continuous integration.
Regenerate from a repeatable command
Put the generator command in project documentation or a script. Run it from the repository root:
dart run pigeon --input pigeons/messages.dartThe command must use the same input path and output settings that the project expects. If the schema changes, regenerate before committing. A normal change should include the schema and its corresponding generated output.
Detect stale output in continuous integration
Continuous integration (CI) is an automated build service that checks changes before they merge. CI can regenerate the files and fail if regeneration changes the working tree:
#!/usr/bin/env bash
set -euo pipefail
dart pub get
dart run pigeon --input pigeons/messages.dart
git diff --exit-codeA nonzero exit code means that the checked-in generated files do not match the schema or the installed generator. This catches several common errors:
- A developer changed the schema but forgot to regenerate.
- Only one platform's output was regenerated.
- A developer used a different Pigeon version.
- Generated files were edited manually.
- A merge conflict left generated code inconsistent.
Run formatting and tests after this check. A clean generation diff proves consistency, but it does not prove that the native implementations still satisfy the generated interfaces.
Review generated diffs carefully
Generated changes can be large. Reviewers should first inspect the schema diff, then confirm that the generated diff reflects it. Look especially for:
| Schema change | Generated change to expect |
|---|---|
| Rename a method | Matching method names in Dart, Kotlin, and Swift |
| Add a nullable field | Nullable or optional types on every target |
| Change an enum | Matching enum values in every generated API |
| Add a method | A new method in the generated client and native interface |
Do not hide generated changes with a broad ignore rule. If generated files are ignored, a clean checkout may not contain the files required by the platform build.
> callout note > When a generated file conflicts during a merge, resolve the schema first. Then regenerate the outputs instead of manually combining generated source. > ```
Exercise
Exercise
A pull request changes BatteryInfo in pigeons/messages.dart, but the generated Dart file is unchanged. What should you do?
Show answer
Run the pinned Pigeon command, inspect the generated diff, commit the updated outputs with the schema change, and let CI verify that regeneration is clean.