Sub topic 8
Returning Results and Handling Method Errors
Assume you know intermediate Dart, Future, async and await, and basic Kotlin or Swift. This chapter focuses on the result contract between Dart and native code. A result contract defines which values and errors a channel method can return.
The three outcomes of a method call
A native method handler should complete every call with exactly one outcome:
success(value)returns a supported value to Dart.error(code, message, details)reports a known failure.notImplemented()tells Dart that the method is unavailable on this platform.
Dart receives a successful value as the result of invokeMethod. Native errors become PlatformException. An unavailable method can become MissingPluginException.
| Native response | Dart outcome | Typical use |
|---|---|---|
| `success(value)` | Returned value | Valid request |
| `error(...)` | `PlatformException` | Invalid input or runtime failure |
| `notImplemented()` | `MissingPluginException` | Unsupported platform |
Return success or an error
The native side must validate arguments before using them. This Kotlin handler returns a greeting only when name is a non-empty string.
MethodChannel(
flutterEngine.dartExecutor.binaryMessenger,
"com.example.app/greeting"
).setMethodCallHandler { call, result ->
if (call.method != "greet") {
result.notImplemented()
return@setMethodCallHandler
}
val name = call.argument<String>("name")
if (name.isNullOrBlank()) {
result.error(
"INVALID_ARGUMENT",
"name must not be empty",
null
)
return@setMethodCallHandler
}
result.success("Hello, $name")
}The error code should be stable and machine-readable. The message is useful for logs and debugging. Details can contain structured information, such as a failing identifier, but do not place secrets in it.
On iOS, the equivalent handler calls its reply closure with a value for success, FlutterError for failure, or FlutterMethodNotImplemented for an unsupported method. Both platforms should implement the same method names, argument keys, result types, and error codes.
Handle errors in Dart
A Dart wrapper should translate channel behavior into a clear application-level API.
import 'package:flutter/services.dart';
class GreetingApi {
static const _channel = MethodChannel('com.example.app/greeting');
Future<String> greet(String name) async {
try {
final value = await _channel.invokeMethod<String>('greet', {
'name': name,
});
return value ?? '';
} on PlatformException catch (error) {
if (error.code == 'INVALID_ARGUMENT') {
throw ArgumentError(error.message ?? 'Invalid name');
}
rethrow;
} on MissingPluginException {
throw StateError('Greeting is unavailable on this platform');
}
}
}Catch PlatformException when the app can recover or show a useful message. Re-throw unknown errors so programming mistakes do not look like expected input failures.
Trace the result path
Exercise
Which native response should a handler use when the method name is unknown, and which Dart exception represents it?
Show answer
Use result.notImplemented(). Dart receives a MissingPluginException.