These examples show communication between the Flutter Application and the host / native application via message channels. The same Flutter code is used for all three embeddings with a minor switch statement for Web communication.
This application uses message channels for mobile platform/Flutter communication. Both Flutter and Native can initiate an increment operation. The native side always receives a message from Flutter after an increment operation has occured.
graph TB;
subgraph host
HostControl[Control and UI Listener]
HostChannel[Message Channel]
HostChannel2[Message Channel]
HostListener[Message Listener]
end
subgraph flutter
FlutterControl[Control and UI Listener]
FlutterChannel[Message Channel]
FlutterChannel2[Message Channel]
FlutterListener[Message Listener]
Counter
end
FlutterControl--increment()-->Counter--{action:incremented}-->FlutterChannel2-.{action:incremented}.->HostChannel2-->HostListener
HostControl--increment()-->HostChannel-."{action:incremnt}".->FlutterChannel
FlutterChannel--"{action:increment}"-->FlutterListener--increment()-->Counter
Additional details are available for each platform implementation
Communication between the Mobile native code and Flutter happens over platform channels. Web commnication happens via window messaging
There are three native platform channel types. V1 of this application uses the Message Channel
Channel type and Flutter class | Description | Flutter Native | Native Flutter | Return |
---|---|---|---|---|
Method Invocation | Invoke method on the other side | Yes | Yes | Via result |
Message | Sends a message to a remote listener | Yes | Yes | Via reply |
Event | Streams and sinks. Events can flow in both directions | Yes | Yes | N/a |
Dart/Flutter, Android and iOS have corresponding channel
classes for each channel type. The three platform channel implementation classes in Flutter, iOS and, Android are:
The V1 demonstration code in this repository implements native communication via a message channel
.
Flutter Class | iOS Class | Android Class |
---|---|---|
MethodChannel | FlutterMethodChanel | MethodChannel |
EventChannel | FlutterEventChannel | EventChannel |
BasicMessageChannel | FlutterBasicMessageChannel | BasicMessageChannel |
Method channels a method on the opposite side. Method channels use MethodCode implementations.
Flutter Codec Class | iOS Codec Class | Android Codec Class |
---|---|---|
MethodCodec Interface | ... | ... |
StandardMethodCodec | FlutterStandardMethodCodec | StandardMethodCodec |
JSONMethodCodec | FlutterJSONMethodCodec | JSONMethodCodec |
Message channels use MessageCodec Implementations. Messages are a single payload with an optional return value. Uses codecs shown below.
Flutter Codec Class | iOS Codec Class | Android Codec Class |
---|---|---|
MessageCodec Interface | ... | ... |
StandardMessageCodec | FlutterStandardMessageCodec | StandardMessageCodec |
BinaryCodec | FlutterBinaryMessageCodec | BinaryCodec |
JsonMessageCodec | FlutterJSONMessageCodec | JSONMessageCodec |
StringCodec | FlutterStringCodec | StringCodec |
Event channels are continuous broadcast streams that use a MethodCodec
.
Flutter Codec Class | iOS Codec Class | Android Codec Class |
---|---|---|
See MethodCodec | See MethodCodec | See MethodCodec |
This is the shortest path with as little code as possible.
- Flutter fills up the entire screen for the iOS and Android applications. There are no native controls to generate the increment messages
- This uses a classic single channel specifying the action in a field in the JSON. Native applications typically do one message per channel and have one channel for each message type