Sub topic 9
Reporting Errors Back to Dart
Pigeon gives errors a typed boundary, but it does not remove the need for an error policy. Native code can fail because input is invalid, a permission is missing, a service is unavailable, or an unexpected platform error occurs. Decide which failures are expected before writing the API.
How Pigeon transports errors
A generated Pigeon handler catches failures from the native implementation and sends an error response through the platform channel. The generated Dart method then completes its Future with a PlatformException.
The exception normally contains:
| Field | Meaning |
|---|---|
| `code` | A stable identifier for the failure kind |
| `message` | A readable description |
| `details` | Optional structured context |
Dart should branch on the stable code, not on the human-readable message. Messages can change without changing the meaning of an error.
sequenceDiagram
participant D as Dart
participant P as Pigeon handler
participant N as Native implementation
D->>P: getSecureValue()
P->>N: Call generated API
N-->>P: Throw expected error
P-->>D: Error response
D-->>D: Future completes with PlatformExceptionThrowing expected errors on the native side
Use a small set of documented error codes. For example, a native implementation might reject a request when a required permission is unavailable:
class SecureValueApiImpl(
private val hasPermission: () -> Boolean,
) : SecureValueApi {
override fun getSecureValue(): String {
if (!hasPermission()) {
throw IllegalStateException("permission_denied")
}
return "value-from-the-platform"
}
}The generated Pigeon code catches the thrown failure and converts it into a platform error. The exact native exception type and generated method signature depend on the Pigeon version and the selected platform. Keep the failure condition in your implementation, and let the generated wrapper transport it.
For public APIs, prefer a native error type or wrapper that preserves a stable code and safe details when your generated Pigeon version supports that pattern. Do not expose stack traces, access tokens, file contents, or other private data in an error message.
Handling the error in Dart
Catch PlatformException around the generated call. Convert platform-specific failures into application-level results at the edge of your Dart code:
import 'package:flutter/services.dart';
import 'src/messages.g.dart';
sealed class SecureValueResult {
const SecureValueResult();
}
class SecureValueLoaded extends SecureValueResult {
const SecureValueLoaded(this.value);
final String value;
}
class SecureValueUnavailable extends SecureValueResult {
const SecureValueUnavailable(this.reason);
final String reason;
}
Future<SecureValueResult> loadSecureValue(SecureValueApi api) async {
try {
final value = await api.getSecureValue();
return SecureValueLoaded(value);
} on PlatformException catch (error) {
switch (error.code) {
case 'permission_denied':
return const SecureValueUnavailable(
'Allow the required permission and try again.',
);
case 'service_unavailable':
return const SecureValueUnavailable(
'The platform service is unavailable.',
);
default:
return const SecureValueUnavailable(
'The value could not be loaded.',
);
}
}
}This keeps PlatformException out of most widgets and business logic. A screen can show a useful message without knowing whether the failure came from Android, iOS, or another host platform.
Expected and unexpected failures
Expected failures are conditions the application can handle, such as denied permission or an unavailable sensor. Give them specific codes.
Unexpected failures indicate a programming error or an operating-system condition you did not plan for. Log enough diagnostic information on the native side, but return a generic safe message to Dart. Never make Dart depend on exception class names or native stack traces.
A nullable return value is not always a good substitute for an error. null cannot distinguish “there is no value” from “the operation failed.” Use a nullable field only when absence is a normal result.
Exercise
Why should Dart switch on PlatformException.code instead of PlatformException.message?
Show answer
The code is a stable machine-readable identifier. The message is for people and may change between releases or platforms.