Sub topic 1
How platform channels work and where they break
Prerequisites
This chapter assumes you know basic Dart and Flutter app structure, and that you can use async and await with Futures. Reading Kotlin or Swift helps but is not required. You should also know that Flutter apps reach native code through platform channels.
A platform channel is a message path between Dart and native code. Dart sends a message that names a channel and a method. The native side listens on the same channel name, runs its code, and returns a reply. The most common channel type is MethodChannel:
import 'package:flutter/services.dart';
const channel = MethodChannel('com.example.app/battery');
Future<int> getBatteryLevel() async {
final level = await channel.invokeMethod<int>('getBatteryLevel');
return level ?? -1;
}Where channels break
A MethodChannel has no types. The channel name, method name, argument keys and value types are all plain strings or untyped values, so the compiler cannot check them. Common failures include:
- A typo in the channel or method name fails only when the app runs.
- A misspelled map key returns
nullwithout an error. - Dart sends an
int, but native code reads it as a different number type, which can throw at runtime. - Each platform writes its own parser, so the Dart and native sides can drift apart.
What Pigeon generates and why use it
Pigeon is a code generator. You describe the API once in a Dart file. Pigeon then writes matching Dart, Kotlin and Swift code. The generated code handles the channel names, serialization and type conversion. Your app code calls ordinary typed methods, and the native side implements typed interfaces that the compiler checks.
flowchart TD
A[Pigeon input file in Dart] --> B[Pigeon generator]
B --> C[Generated Dart client]
B --> D[Generated Kotlin interface]
B --> E[Generated Swift protocol]caption: Pigeon turns one Dart input file into Dart, Kotlin and Swift code.
alt: A flowchart where a Pigeon input file feeds the Pigeon generator, which produces three outputs: a Dart client, a Kotlin interface and a Swift protocol.
Adding Pigeon and generating code
Add Pigeon as a dev dependency, because only the generated files ship in your app. Replace the placeholder with the current version from pub.dev:
dev_dependencies:
pigeon: ^latest_versionCreate pigeons/messages.dart with the output paths and your API. Then run the generator:
dart run pigeon --input pigeons/messages.dartRun this command again whenever the input file changes.
Describing data: classes, enums and nullable fields
Pigeon data types are plain Dart classes and enums. A field marked with ? is nullable, and Pigeon carries that through to Kotlin and Swift:
enum BatteryState { charging, discharging, full, unknown }
class BatteryInfo {
BatteryInfo({required this.level, this.state});
int level;
BatteryState? state;
}A nullable field may be null when it crosses the boundary, so native code must handle that case. Use ? only when a value can truly be missing.
Exercise
Exercise
A MethodChannel call sends a map with the key "level", but native code reads "lvl". Why does this bug survive compilation, and what does Pigeon change?
Show answer
Maps are untyped, so the compiler cannot check key names. The app fails only at runtime, or returns null silently. With Pigeon, the key is a typed field of a generated class, so a misspelled name fails at compile time on both the Dart and native sides.