Part 3 · 1 chapters · ~8 min

Platform Channels

MethodChannel, EventChannel and BasicMessageChannel, codecs, threading on the native side, Pigeon for typed interfaces, dart:ffi and ffigen for C libraries, writing a plugin, platform views for embedding native UI and their cost, and handling permissions, payments SDKs and push notifications.

5

Talking to the platform

code
// Dart
const _ch = MethodChannel('app/battery');
final level = await _ch.invokeMethod<int>('level');

// Kotlin (Android)
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, "app/battery").setMethodCallHandler { call, result ->
  if (call.method == "level") {
    val bm = getSystemService(BATTERY_SERVICE) as BatteryManager
    result.success(bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY))
  } else result.notImplemented()
}

// Pigeon: one typed definition generates Dart, Kotlin and Swift
@HostApi() abstract class PaymentsApi { @async ChargeResult charge(ChargeRequest req); }

Payment and KYC SDKs (Paystack, Flutterwave, Smile ID) often ship native SDKs wrapped by Flutter plugins; check how they handle activity results, background termination and retries, because a payment that succeeds natively while the Dart side was killed needs server-side reconciliation (Ledgers course).

A PLATFORM CHANNEL CALL
Dart asks Android for the battery level
Dart (UI isolate)engineKotlin / Swift handlerMethodChannel("battery").invokeMethod("level")encoded message on the platform thread
swipe the figure sideways, or tap expand for full screen
1/4
messages, not calls
Platform channels send asynchronous messages encoded with a codec (StandardMessageCodec) between Dart and the platform thread.
async encoded messagesStandardMessageCodec