Sub topic 3
Adding Pigeon and Generating Code
Assume you know basic Flutter project structure, Dart async and await, and the purpose of Android and iOS platform code. You should also know that Flutter communicates with native code through platform channels. This chapter focuses on installing Pigeon, defining its inputs, and running code generation.
Add Pigeon as a development dependency
Pigeon is a Dart package used while developing the application or plugin. It reads a Dart schema and generates source files for each platform. The generated files are used at runtime, but the Pigeon package itself is normally only needed to run the generator.
Add it to dev_dependencies in pubspec.yaml:
dev_dependencies:
pigeon: ^latest_versionReplace latest_version with the current compatible version from pub.dev. Then fetch the package:
flutter pub getA development dependency keeps the runtime dependency list smaller. It also makes the code-generation step explicit in the project setup.
Create the Pigeon input file
Create a directory such as pigeons/, then add pigeons/messages.dart. This file is the source of truth for the API. It contains the output paths, shared data types, and API declarations.
import 'package:pigeon/pigeon.dart';
@ConfigurePigeon(PigeonOptions(
dartOut: 'lib/src/messages.g.dart',
kotlinOut: 'android/app/src/main/kotlin/com/example/app/Messages.g.kt',
kotlinOptions: KotlinOptions(package: 'com.example.app'),
swiftOut: 'ios/Runner/Messages.g.swift',
))
enum BatteryState { charging, discharging, full, unknown }
class BatteryInfo {
BatteryInfo({required this.level, required this.state});
int level;
BatteryState state;
}
@HostApi()
abstract class BatteryApi {
BatteryInfo getBatteryInfo();
}@ConfigurePigeon tells Pigeon where to write each generated file. The Kotlin package must match the package used by the Android source. The output directories must already fit the structure of the Flutter project.
BatteryState and BatteryInfo are shared types. BatteryApi is a HostApi, which means Dart will call an implementation supplied by Android or iOS. The native implementations are added later. At this stage, the schema only describes the contract.
Run the generator
Run this command from the Flutter project root:
dart run pigeon --input pigeons/messages.dartPigeon reads the input file and creates the configured outputs:
| Output | Purpose |
|---|---|
| `lib/src/messages.g.dart` | Typed Dart API and data classes |
| `android/.../Messages.g.kt` | Kotlin data types and API interfaces |
| `ios/Runner/Messages.g.swift` | Swift data types, protocols, and message wiring |
The generated Dart class exposes BatteryApi methods as typed calls. The generated Kotlin and Swift files provide matching native contracts and the platform-channel transport code. Application code no longer needs to repeat channel names or manually cast message values.
Check generated files into the project
Generated files are part of the application build, so teams commonly commit them to version control. This lets a clean checkout build without requiring every developer or build machine to know the generation command. The input schema remains the authoritative source.
Regenerate files whenever the schema changes. Review generated-file changes together with the schema change. Do not edit generated files by hand because the next generation run will overwrite those edits.
Exercise
Exercise
Which three things must agree before generated Kotlin code can be used safely: the Kotlin package option, the output path, or the native source package?
Show answer
All three must agree. The package option and native source package must match, and the output path must place the file in the expected Android source tree.