D
Flutter CoreAdvanced50 XP7 min read

What are Platform Channels and how do you use them?

TL;DR: Platform Channels let Flutter communicate with native Android/iOS code using message-passing over MethodChannel (request-response), EventChannel (streams), or BasicMessageChannel (custom codec).

Full Answer

MethodChannel

MethodChannel supports a request-response pattern. Flutter invokes a method by name, native code handles it and returns a result. Data is encoded using StandardMessageCodec (supports primitives, lists, maps).

EventChannel

EventChannel models a stream of events from native to Flutter. Used for continuous data like sensor readings, location updates, or battery level changes.

BasicMessageChannel

Bidirectional messaging with a custom codec. Use when MethodChannel's codec doesn't support your data types, or for two-way continuous communication.

🎯

Platform channel calls happen on the main thread on iOS/Android by default. For heavy computation, dispatch to a background thread on the native side using Handler (Android) or DispatchQueue (iOS).

Code Examples

dartMethodChannel: calling native battery level
Output
Returns '87%' (or whatever the device battery level is).
kotlinAndroid: MethodChannel handler in MainActivity
Output
Responds to 'getBatteryLevel' method call with an integer 0-100.

Interview Tip

💡

When asked about platform channels, mention thread safety: always invoke platform channels from the main thread in Dart. For heavy native work, run it on a background thread/queue on the native side before returning.

#platform-channels#method-channel#event-channel#native#interop