Understanding Flutter Platform Channels: Communication Between Dart and Native Android
This article explains how Flutter’s PlatformChannel works, detailing the communication flow from Dart to Android and back, including the role of MethodChannel, BasicMessageChannel, and EventChannel, with code examples and a step‑by‑step analysis of the underlying engine implementation.
Flutter provides a PlatformChannel mechanism that enables Dart code to communicate with native Android (or iOS) code.
The article first explains the need for PlatformChannel, then introduces three channel types defined in platform_channel.dart : BasicMessageChannel , MethodChannel , and EventChannel , each with properties name , codec , and binaryMessenger .
Examples show how to use MethodChannel to invoke Android methods, how Android registers the channel with MethodChannel and handles calls, and how to send responses back to Dart.
Similar examples demonstrate sending messages from Dart to Android with BasicMessageChannel and receiving stream events with EventChannel , including timer‑based event emission on the native side.
The article then dives into the Flutter engine source, tracing the call flow from MethodChannel.invokeMethod → BinaryMessenger.send → window.sendPlatformMessage → native _sendPlatformMessage in window.cc , through RuntimeController , Engine , Shell , and finally to the Android FlutterView.handlePlatformMessage JNI method, which forwards the message to the Java FlutterJNI.handlePlatformMessage implementation.
On the Java side, the message is dispatched to the registered channel handler, and the reply is sent back via invokePlatformMessageEmptyResponseCallback if no handler is present.
The complete flow demonstrates how Flutter’s Dart messenger, engine, and Android embedding cooperate to achieve seamless cross‑language communication.
360 Tech Engineering
Official tech channel of 360, building the most professional technology aggregation platform for the brand.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.