Advanced
Accessing Native Code with Platform Channels in Flutter
· 9 min read
When Do You Need a Platform Channel?
There are thousands of packages on pub.dev, but sometimes what you need just isn't there: wrapping a company-internal SDK, accessing a device-specific API, or integrating an existing native library into Flutter. A platform channel is the bridge that passes asynchronous messages between your Dart code and the platform's own language (Kotlin/Java on Android, Swift/Objective-C on iOS).
MethodChannel: The Dart Side
A MethodChannel is identified by a name (usually a string resembling a package name); this name must match exactly on both the Dart and native side. You call invokeMethod with a method name and optional arguments, and get back a Future.
class PilServisi {
static const _kanal = MethodChannel('com.muhammet.app/pil');
static Future<int> pilSeviyesiAl() async {
try {
final int seviye = await _kanal.invokeMethod('pilSeviyesiAl');
return seviye;
} on PlatformException catch (e) {
throw Exception('Pil seviyesi alinamadi: ${e.message}');
}
}
}MethodChannel: The Android Side (Kotlin)
In MainActivity.kt, you register a MethodCallHandler under the same channel name. The incoming call lets you check which method was invoked via call.method, and send a response back to Dart with result.
class MainActivity : FlutterActivity() {
private val KANAL = "com.muhammet.app/pil"
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, KANAL).setMethodCallHandler { call, result ->
if (call.method == "pilSeviyesiAl") {
val seviye = getPilSeviyesi()
if (seviye != -1) {
result.success(seviye)
} else {
result.error("KULLANILAMIYOR", "Pil seviyesi okunamadi", null)
}
} else {
result.notImplemented()
}
}
}
}MethodChannel: The iOS Side (Swift)
The structure in AppDelegate.swift is conceptually identical; a FlutterMethodChannel is registered, and incoming calls are handled with setMethodCallHandler.
let kanal = FlutterMethodChannel(name: "com.muhammet.app/pil",
binaryMessenger: controller.binaryMessenger)
kanal.setMethodCallHandler { call, result in
if call.method == "pilSeviyesiAl" {
result(self.pilSeviyesiniAl())
} else {
result(FlutterMethodNotImplemented)
}
}EventChannel: Continuous Data Streams
MethodChannel is for one-shot request-response calls. For continuously flowing data like sensor readings or battery level changes, you use an EventChannel instead; on the Dart side, this shows up as a Stream.
const _olayKanali = EventChannel('com.muhammet.app/pil_degisimi');
Stream<int> pilDegisimiStream() {
return _olayKanali.receiveBroadcastStream().map((olay) => olay as int);
}Things to Watch Out For
1. Channel Names Must Match Exactly
Even a single character difference between the Dart and native channel name causes a 'MissingPluginException'. Using a reverse-domain format (com.company.app/feature) is a common convention to avoid collisions.
2. Only Simple Types Can Be Sent
Platform channels communicate over StandardMessageCodec, which only supports simple types like null, bool, int, double, String, List and Map. You need to convert your own classes into a Map before sending them.
Conclusion
Platform channels might look like an exception to Flutter's 'write everything from a single codebase' promise, but they're really what completes it: they make native APIs reachable even when there's no ready-made plugin. MethodChannel is the right tool for one-shot calls, EventChannel for continuous data streams; keep channel names consistent and handle errors properly with PlatformException.