Messaging
How are messages passed between the framework and platform code?
- ServicesBinding.initInstancessets the global message handler (- Window.onPlatformMessage) to- ServicesBinding.defaultBinaryMessenger. This instance processes messages from the platform (via- BinaryMessenger.handlePlatformMessage) and allows other framework code to register message handlers (via- BinaryMessenger.setMessageHandler). Handlers subscribe to a channel (an identifier used to multiplex the single engine callback) using an identifier shared by the framework and the platform.
What are the building blocks of messaging?
- BinaryMessengermultiplexes the global message handler via channel names, supporting handler registration and bidirectional binary messaging. Sending a message produces a future that resolves to the raw response.
- MessageCodecdefines an interface to encode and decode byte data (- MessageCodec.encodeMessage,- MessageCodec.decodeMessage). A cross-platform binary codec is available (- StandardMessageCodec) as well as a JSON-based codec (- JSONMessageCodec). The platform must implement a corresponding codec natively.
- MethodCodecis analogous to- MessageCodec(but otherwise independent) encoding and decoding- MethodCallinstances that wrap a method name and a dynamic list of arguments. Method-based codecs pack and unpack results into envelopes to distinguish success and error outcomes.
- BasicMessageChannelprovides a thin wrapper around- BinaryMessagerthat uses the provided codec to encode and decode messages to and from raw byte data.
- MethodChannelprovides a thin wrapper around- BinaryMessagerthat uses the provided method codec to encode and decode method invocations. Responses to incoming invocations are packed into envelopes indicating outcome; similarly, results from outgoing invocations are unpacked from their encoded envelope. These are returned as futures.- Success envelopes are unpacked and the result returned. 
- Error envelopes throw a - PlatformException.
- Unrecognized methods throw a - MissingPluginException(except when using an- OptionalMethodChannel).
 
- EventChannelis a helper that exposes a remote stream as a local stream. The initial subscription is handled by invoking a remote method called- listen(via- MethodChannel.invokeMethod) which causes the platform to begin emitting a stream of envelope-encoded items. A top-level handler is installed (via- ServicesBinding.defaultBinaryMessenger.setMessageHandler) to unpack and forward items to an output stream in the framework. If the stream ends (for any reason), a remote method called- cancelis invoked and the global handler cleared.
- SystemChannelsis a singleton instance that provides references to messaging channels that are essential to the framework (- SystemChannels.system,- SystemChannels.keyEvent, etc.).
Last updated
Was this helpful?