Why Platform Channels?
Flutter has packages for most needs. Sometimes you need a feature that has no package, or you must use your company's native SDK. A platform channel lets Dart code send a message to native code (Kotlin or Swift) and get an answer back.
Check pub.dev first: Before you write native code, search for a package. Camera, location, Bluetooth, biometrics and sharing are all available.
How It Works
text
Dart --- "getBatteryLevel" ---> Kotlin / Swift
Dart <--- 87 ------------------- Kotlin / Swift
Both sides use a channel with the same name. The messages are asynchronous.
Dart Side
dart
import 'package:flutter/services.dart';
class BatteryService {
static const _channel = MethodChannel('com.example.app/battery');
Future<int?> getBatteryLevel() async {
try {
final level = await _channel.invokeMethod<int>('getBatteryLevel');
return level;
} on PlatformException catch (e) {
debugPrint('Failed: ${e.message}');
return null;
} on MissingPluginException {
return null; // platform has no implementation
}
}
}
Android Side (Kotlin)
Open android/app/src/main/kotlin/.../MainActivity.kt.
kotlin
import android.content.Context
import android.os.BatteryManager
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
class MainActivity : FlutterActivity() {
private val CHANNEL = "com.example.app/battery"
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL)
.setMethodCallHandler { call, result ->
if (call.method == "getBatteryLevel") {
val manager = getSystemService(Context.BATTERY_SERVICE) as BatteryManager
val level = manager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY)
result.success(level)
} else {
result.notImplemented()
}
}
}
}
iOS Side (Swift)
Open ios/Runner/AppDelegate.swift.
swift
import UIKit
import Flutter
@main
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
let controller = window?.rootViewController as! FlutterViewController
let channel = FlutterMethodChannel(name: "com.example.app/battery",
binaryMessenger: controller.binaryMessenger)
channel.setMethodCallHandler { call, result in
if call.method == "getBatteryLevel" {
UIDevice.current.isBatteryMonitoringEnabled = true
let level = Int(UIDevice.current.batteryLevel * 100)
result(level)
} else {
result(FlutterMethodNotImplemented)
}
}
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
Project templates change: The exact startup code in AppDelegate.swift and MainActivity.kt differs between Flutter versions. Compare with the official platform channel guide on docs.flutter.dev if your file looks different.
Channel Types
| Type | Use |
MethodChannel | Call a function and get one result |
EventChannel | Continuous stream of events, such as sensor data |
BasicMessageChannel | Simple two-way messages |
Better Alternatives for Larger Work
- Pigeon: generates type-safe channel code
- FFI (dart:ffi): call C or C++ libraries directly
- Federated plugins: turn your code into a reusable package
Conclusion
Platform channels let Flutter use any native capability. Use them sparingly, keep the API small and look for a package first.
Quick Summary: MethodChannel with the same name on Dart and native sides, invokeMethod from Dart and setMethodCallHandler in Kotlin or Swift.