Sub topic 2
What Pigeon Generates and Why Use It
Assume you know basic Dart and Flutter app structure, can use async and await, and have seen Flutter platform channels. You should also be able to read simple Kotlin or Swift. This chapter explains the problem Pigeon solves and the files it creates.
The problem with hand-written channels
A MethodChannel sends method names and arguments across Flutter's platform boundary. The Dart and native sides must agree on every detail manually.
const channel = MethodChannel('battery');
Future<int> readBatteryLevel() async {
final result = await channel.invokeMethod<int>('getBatteryLevel');
return result ?? 0;
}The Android or iOS implementation must use exactly the same channel name and method name. It must also return a value with the type that Dart expects. A typo or an incompatible value usually becomes a runtime error.
Common failure points include:
- A different channel name on each side.
- A misspelled method name.
- A missing argument or an argument with the wrong type.
- A native result that Dart casts incorrectly.
- Data classes represented as loosely structured maps.
- Exceptions that cross the boundary without a consistent design.
The compiler cannot check the agreement between the Dart string and the native string. Tests can find some mistakes, but only after the code runs.
What Pigeon generates
Pigeon is a code generator. You describe an interface in a Dart schema, then Pigeon creates matching code for each supported platform.
The generated code usually contains these parts:
- A Dart API class with typed methods.
- Dart data classes and enums.
- A message codec that serializes values across the channel.
- A Kotlin interface for Android.
- A Swift protocol for iOS.
- Registration helpers that connect implementations to Flutter's binary messenger.
- Native client code for calls from native code back to Dart.
A binary messenger is Flutter's low-level transport for encoded messages. Pigeon uses it underneath, but your application code calls generated methods instead of constructing channel messages itself.
The generated files are ordinary source code. You normally do not edit them directly. Instead, you edit the schema and run Pigeon again.
Typed contracts reduce risk
A schema can define shared data:
enum BatteryState { charging, discharging, full, unknown }
class BatteryInfo {
BatteryInfo({required this.level, required this.state});
int level;
BatteryState state;
}It can then define a host API. A host API is an interface implemented by the native platform and called by Dart.
@HostApi()
abstract class BatteryApi {
BatteryInfo getBatteryInfo();
}Pigeon turns this declaration into a typed Dart method and matching native declarations. Dart code can then call the generated API:
final battery = BatteryApi();
final info = await battery.getBatteryInfo();
print('${info.level}%: ${info.state.name}');The generated method returns a Future because communication with native code is asynchronous, even when the native implementation performs a quick operation. The generated codec also knows how to encode BatteryInfo, BatteryState, integers, strings, lists, maps, and other supported values.
> callout note > Pigeon improves the contract between Dart and native code, but it does not validate the behavior of the native implementation. A native method can still return an incorrect battery level or fail at runtime.
Why use Pigeon?
Pigeon is most useful when an API has several methods or shares structured data between platforms. Its main benefits are:
- Compile-time feedback: Many mismatches become build errors instead of runtime cast errors.
- One source of truth: The schema documents the API and drives all generated declarations.
- Less boilerplate: You do not repeat channel names, method strings, and serialization code.
- Consistent data encoding: Both sides use generated codecs for declared types.
- Clear platform boundaries: Native implementations focus on platform behavior, while the schema defines the contract.
Pigeon does not remove platform channels. It generates the channel code around them. Use a hand-written channel for a very small, private message or an API whose shape changes constantly. Use Pigeon when type safety, shared models, and long-term maintenance matter.
Exercise
What error does Pigeon mainly prevent when replacing a hand-written MethodChannel?
Show answer
It prevents many mismatches between Dart and native code, such as wrong method names, incompatible argument types, and inconsistent result types, by generating both sides from one typed schema.