0PricingLogin
Flutter Mobile Development · Lesson

Type-Safe Platform Channels with Pigeon

Generate strongly typed host and Flutter messaging interfaces using the Pigeon tool.

What is Pigeon and Why Use It?

Platform channels in Flutter traditionally use string-based method names and untyped dynamic maps. A typo in a method name or a mismatched argument type only fails at runtime — often on a device you don't own.

Pigeon is a code-generation tool from the Flutter team that solves this problem. You define a Dart API file describing the messages and host APIs, and Pigeon generates:

  • Type-safe Dart classes and abstract channel stubs
  • Matching native code for Android (Kotlin/Java) and iOS (Swift/ObjC)

The result is a compile-time-checked contract between Flutter and the host platform — no raw strings, no untyped maps.

Adding Pigeon to Your Project

Pigeon is a dev-only dependency. Add it to pubspec.yaml under dev_dependencies:

You also need the Flutter standard method codec, which Pigeon uses internally — it is already part of the Flutter SDK, so no extra package is required.

After adding the dependency, run flutter pub get. Pigeon is invoked via dart run pigeon (not as a build_runner builder), which gives you full control over when code is regenerated.

# pubspec.yaml (relevant excerpt)
# dev_dependencies:
#   pigeon: ^22.0.0

// Run generation (in terminal, not Dart code):
// dart run pigeon --input pigeons/messages.dart

All lessons in this course

  1. Calling C Libraries with dart:ffi
  2. Type-Safe Platform Channels with Pigeon
  3. Writing Custom Platform Plugins for iOS and Android
  4. Background Isolates and Native Memory Management
← Back to Flutter Mobile Development