Sub topic 7
Handling Method Calls on Android and iOS
Assume intermediate Dart and Flutter knowledge, including Future, asynchronous code, and widget lifecycles. Also assume basic Kotlin or Swift syntax. This chapter focuses on the native side of a MethodChannel: receiving a call, validating its arguments, and returning exactly one result.
A method call follows this path:
The handler contract
Both Android and iOS receive the method name and arguments. The handler must then do one of three things:
- Return a value with a success result.
- Return a structured error when input or native work fails.
- Report an unsupported method.
The channel name must match the Dart channel name exactly. A spelling or capitalization difference creates two unrelated channels.
Handling calls on Android
Register the handler when the Flutter engine is configured. This example accepts a name string and returns a greeting.
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
class MainActivity : FlutterActivity() {
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
MethodChannel(
flutterEngine.dartExecutor.binaryMessenger,
"com.example.app/greeting"
).setMethodCallHandler { call, result ->
when (call.method) {
"greet" -> {
val name = call.argument<String>("name")
if (name.isNullOrBlank()) {
result.error(
"INVALID_ARGUMENT",
"name is required",
null
)
} else {
result.success("Hello, $name")
}
}
else -> result.notImplemented()
}
}
}
}call.argument<String>("name") reads a value decoded from Dart. Always validate nullable or externally supplied values before using them. Call result.success, result.error, or result.notImplemented once for each call. Calling a result twice can produce inconsistent behavior.
Handling calls on iOS
iOS uses a FlutterMethodChannel and a closure with the same conceptual contract.
import Flutter
import UIKit
@UIApplicationMain
class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
guard let controller = window?.rootViewController as? FlutterViewController else {
return false
}
let channel = FlutterMethodChannel(
name: "com.example.app/greeting",
binaryMessenger: controller.binaryMessenger
)
channel.setMethodCallHandler { call, result in
guard call.method == "greet" else {
result(FlutterMethodNotImplemented)
return
}
guard
let arguments = call.arguments as? [String: Any],
let name = arguments["name"] as? String,
!name.trimmingCharacters(in: .whitespaces).isEmpty
else {
result(FlutterError(
code: "INVALID_ARGUMENT",
message: "name is required",
details: nil
))
return
}
result("Hello, \(name)")
}
return super.application(
application,
didFinishLaunchingWithOptions: launchOptions
)
}
}| Situation | Android result | iOS result |
|---|---|---|
| Valid method | `result.success(value)` | `result(value)` |
| Invalid input | `result.error(...)` | `result(FlutterError(...))` |
| Unknown method | `result.notImplemented()` | `result(FlutterMethodNotImplemented)` |
Exercise
What should a native handler return when the method name is unknown?
Show answer
It should return the platform’s not-implemented result, such as result.notImplemented() on Android or FlutterMethodNotImplemented on iOS.