Pigeon is a code generator tool to make communication between Flutter and the host platform type-safe, easier, and faster.
Pigeon removes the necessity to manage strings across multiple platforms and languages. It also improves efficiency over common method channel patterns. Most importantly though, it removes the need to write custom platform channel code, since pigeon generates it for you.
For usage examples, see the Example README.
Currently pigeon supports generating:
- Kotlin and Java code for Android
- Swift and Objective-C code for iOS and macOS
- C++ code for Windows
- GObject code for Linux
Pigeon uses the StandardMessageCodec
so it supports
any datatype platform channels support.
Custom classes, nested datatypes, and enums are also supported.
Nullable enums in Objective-C generated code will be wrapped in a class to allow for nullability.
By default, custom classes in Swift are defined as structs. Structs don't support some features - recursive data, or Objective-C interop. Use the @SwiftClass annotation when defining the class to generate the data as a Swift class instead.
While all calls across platform channel APIs (such as pigeon methods) are asynchronous, pigeon methods can be written on the native side as synchronous methods, to make it simpler to always reply exactly once.
If asynchronous methods are needed, the @async
annotation can be used. This will require
results or errors to be returned via a provided callback. Example.
All Host API exceptions are translated into Flutter PlatformException
.
- For synchronous methods, thrown exceptions will be caught and translated.
- For asynchronous methods, there is no default exception handling; errors should be returned via the provided callback.
To pass custom details into PlatformException
for error handling,
use FlutterError
in your Host API. Example.
For swift, use PigeonError
instead of FlutterError
when throwing an error. See Example#Swift for more details.
Host API errors can be sent using the provided FlutterError
class (translated into PlatformException
).
For synchronous methods:
- Objective-C - Set the
error
argument to aFlutterError
reference. - C++ - Return a
FlutterError
.
For async methods:
- Return a
FlutterError
through the provided callback.
When targeting a Flutter version that supports the
TaskQueue API
the threading model for handling HostApi methods can be selected with the
TaskQueue
annotation.
Host and Flutter APIs now support the ability to provide a unique message channel suffix string to the api to allow for multiple instances to be created and operate in parallel.
- Add pigeon as a
dev_dependency
. - Make a ".dart" file outside of your "lib" directory for defining the communication interface.
- Run pigeon on your ".dart" file to generate the required Dart and
host-language code:
flutter pub get
thendart run pigeon
with suitable arguments. Example. - Add the generated Dart code to
./lib
for compilation. - Implement the host-language code and add it to your build (see below).
- Call the generated Dart methods.
- The file should contain no method or function definitions, only declarations.
- Custom classes used by APIs are defined as classes with fields of the supported datatypes (see the supported Datatypes section).
- APIs should be defined as an
abstract class
with either@HostApi()
or@FlutterApi()
as metadata.@HostApi()
being for procedures that are defined on the host platform and the@FlutterApi()
for procedures that are defined in Dart. - Method declarations on the API classes should have arguments and a return
value whose types are defined in the file, are supported datatypes, or are
void
. - Generics are supported, but can currently only be used with nullable types
(example:
List<int?>
). - Objc and Swift have special naming conventions that can be utilized with the
@ObjCSelector
and@SwiftFunction
respectively.
- Add the generated Objective-C or Swift code to your Xcode project for compilation
(e.g.
ios/Runner.xcworkspace
or.podspec
). - Implement the generated protocol for handling the calls on iOS, set it up as the handler for the messages.
- Add the generated Java or Kotlin code to your
./android/app/src/main/java
directory for compilation. - Implement the generated Java or Kotlin interface for handling the calls on Android, set it up as the handler for the messages.
- Add the generated C++ code to your
./windows
directory for compilation, and to yourwindows/CMakeLists.txt
file. - Implement the generated C++ abstract class for handling the calls on Windows, set it up as the handler for the messages.
- Add the generated Objective-C or Swift code to your Xcode project for compilation
(e.g.
macos/Runner.xcworkspace
or.podspec
). - Implement the generated protocol for handling the calls on macOS, set it up as the handler for the messages.
- Add the generated GObject code to your
./linux
directory for compilation, and to yourlinux/CMakeLists.txt
file. - Implement the generated protocol for handling the calls on Linux, set it up as the vtable for the API object.
Pigeon also supports calling in the opposite direction. The steps are similar
but reversed. For more information look at the annotation @FlutterApi()
which
denotes APIs that live in Flutter but are invoked from the host platform.
Example.
File an issue in flutter/flutter with "[pigeon]" at the start of the title.