Skip to content
tezvyn:
Menu

Pigeon: Type-Safe Platform Channels in Flutter

Source: github.comMediumHow cards are made

Pigeon acts like a contract for your app's native code, generating type-safe platform channels from a single definition. This replaces manual, error-prone MethodChannel code. The footgun: Swift types default to structs; use @SwiftClass for class features.

Why it exists

Flutter apps often need to talk to native platform APIs, like device sensors or third-party SDKs. The default mechanism, MethodChannel, is string-based and requires manual data serialization. This is error-prone, as a simple typo in a method name or a data type mismatch between Dart and native code can cause runtime failures that are difficult to debug.

The mental model

Think of Pigeon as an Interface Definition Language (IDL) for your app's internal communication. You write a single "contract" file in Dart that defines the functions and data structures. Pigeon then acts as a compiler, taking that contract and generating the type-safe client (Dart) and server (native platform) code, guaranteeing they can talk to each other correctly.

How it works

You create a Dart file defining your API within a class annotated with @HostApi. This class contains the method signatures and data types for your communication. After defining the contract, you run the Pigeon command-line tool. It reads this file and outputs a Dart file for your Flutter app and corresponding files for your native platforms (e.g., Swift for iOS, Kotlin for Android). Your app code calls the generated Dart methods, and your native code implements the generated native interfaces.

When to use it

Use Pigeon for any non-trivial communication between Flutter and the host platform. It's ideal when you need to call native APIs from Dart or have native code trigger actions in your Flutter app. It eliminates manual serialization and the risk of stringly-typed method call errors, making your code more robust and maintainable.

When not to use it

For an extremely simple, one-off platform call where the overhead of setting up the generator feels too high, you might still use a manual MethodChannel. However, for any API that will be maintained or expanded, Pigeon is almost always the better and safer choice.

One canonical example

To get the device's battery level, you'd define a @HostApi() class in your pigeon file with a method like int getBatteryLevel(). After running the generator, you'd get a Dart method getBatteryLevel() to call in your app, and a corresponding native protocol or interface. You would then write the native implementation in Swift or Kotlin that returns the device's battery level as an integer, and Pigeon handles all the plumbing in between.

Interview question

What is the most significant problem Pigeon solves when integrating Flutter with native platform code?

  • a.The need to manually write boilerplate code for simple, one-off platform interactions.
  • b.The challenge of maintaining separate native codebases for iOS and Android within a single project.
  • c.The difficulty of debugging performance bottlenecks in asynchronous platform calls.
  • d.The risk of runtime crashes due to type mismatches or incorrect method names in string-based channels.Correct
Why?

The card explicitly states that MethodChannel is "string-based and requires manual data serialization," leading to "runtime failures" from "typo in a method name or a data type mismatch." Pigeon addresses this by generating "type-safe platform channels" to eliminate these "error-prone" issues. The other options describe problems that are either not Pigeon's primary focus or are explicitly mentioned as scenarios where Pigeon might not be necessary.

Just read this? Test yourself on what you have been reading.

Read the original → github.com

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on flutter — each one lists the topics its interview covers.

See open roles