Sub topic 3
Naming Channels and Registering Handlers
This chapter assumes intermediate Dart and Flutter, including Future, Stream, and asynchronous callbacks. It also assumes basic Kotlin or Swift knowledge. You will learn how a channel name connects Dart code to a native handler and where that handler is registered.
Use a Stable Channel Name
A platform channel is a named communication path between Dart and native code. The Dart side and the native side must use the exact same name, including capitalization and punctuation.
Use a reverse-domain prefix to reduce collisions:
com.example.weather/forecast
com.example.weather/updatesKeep names stable after release. Renaming a channel requires updating every platform implementation and every Dart caller. Give each logical API its own channel, rather than placing unrelated methods on one large channel.
Register the Native Handler
Dart creates a channel object, but the native platform must register a handler for the same name. With the modern Android embedding, registration commonly happens in configureFlutterEngine.
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
class MainActivity : FlutterActivity() {
private val channelName = "com.example.weather/forecast"
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
MethodChannel(
flutterEngine.dartExecutor.binaryMessenger,
channelName
).setMethodCallHandler { call, result ->
when (call.method) {
"getForecast" -> result.success("Sunny")
else -> result.notImplemented()
}
}
}
}The binaryMessenger transports encoded messages between Flutter and Android. The handler receives a method call and must complete its result exactly once. An unknown method should return notImplemented() so Dart can distinguish an unsupported API from an application failure.
The Dart side uses the identical name:
import 'package:flutter/services.dart';
class ForecastApi {
static const _channel = MethodChannel('com.example.weather/forecast');
Future<String> getForecast() async {
final value = await _channel.invokeMethod<String>('getForecast');
return value ?? 'Unknown';
}
}iOS uses the same naming rule. Register a FlutterMethodChannel with the engine’s binary messenger, then attach a method-call handler. The channel name and method names must match the Dart contract on every supported platform.
Registration Scope and Duplicates
Register handlers once for each Flutter engine. Registering the same channel repeatedly can replace an existing handler or produce confusing behavior, especially when an app creates multiple engines. Keep registration near the engine setup, and put platform-specific work behind a small native API rather than inside a widget.
Exercise
What happens if Dart uses com.example.weather/forecast but Android registers com.example.weather/forecasts?
Show answer
They are different channels, so the Dart call has no matching Android handler and typically fails with a missing-plugin or unavailable-channel error.