Sub topic 12
Pitfalls: Debug Versus Release, Missed Errors, and Handler Loops
This chapter assumes that you know basic Dart syntax, async and await, Future, try/catch, and how to build a simple Flutter app. The examples assume that both global handlers are installed in main().
Global error handlers are useful safety nets, but they do not catch every failure. They also behave differently in debug and release builds. Treat a handler as a reporting boundary, not as a replacement for local error handling.
Debug and release builds are different
Flutter displays detailed error information during development. A widget build failure usually produces a red error screen in debug mode. In release mode, the visible result is less useful for diagnosis, and some debugging output is removed or reduced.
debugPrint is useful while developing, but it is not a complete production reporting solution. Send important data to a logging or crash-reporting service in release builds. Avoid sending passwords, tokens, personal data, or entire request bodies.
The handler itself must not depend on debug-only behavior:
class SafeErrorReporter {
static bool _reporting = false;
static void reportFlutterError(FlutterErrorDetails details) {
if (_reporting) {
return;
}
_reporting = true;
try {
debugPrint(details.exceptionAsString());
debugPrintStack(stackTrace: details.stack);
// Send a sanitized event to your reporting service here.
} finally {
_reporting = false;
}
}
static bool reportAsyncError(Object error, StackTrace stack) {
if (_reporting) {
return true;
}
_reporting = true;
try {
debugPrint('Async error: $error');
debugPrint('$stack');
// Send a sanitized event to your reporting service here.
return true;
} finally {
_reporting = false;
}
}
}The _reporting flag prevents a reporting failure from repeatedly entering the same reporting path. In a real app, use a design that is safe for your concurrency model and limits repeated events.
Errors that never reach the handlers
The two global handlers receive errors that remain uncaught in their respective channels. They do not receive every error in the application.
| Situation | Why the global handler may not receive it |
|---|---|
| A `try`/`catch` handles the exception | The error is no longer uncaught |
| A `Future` is awaited inside `try`/`catch` | The surrounding code receives the failure |
| An error occurs in another isolate | That isolate needs an error listener or message channel |
| The operating system kills the process | Dart cannot run cleanup or reporting code |
| A native plugin crashes | A native crash may bypass Dart handlers |
| An error occurs before handler installation | The handlers were not available yet |
An isolate is an independent Dart execution context. Errors from another isolate do not automatically pass through the main isolate's handlers. Attach an error listener when you create or manage an isolate, and forward a sanitized event to your reporting system.
Some failures are also not exceptions. An out-of-memory termination, forced process kill, device power loss, or native segmentation fault may leave no opportunity for Dart code to run.
Prevent loops and false success
A handler can fail while it is logging an error. For example, a reporter might use a broken network client, access an unavailable service, or throw while serializing an object. If the handler reports its own failure through the same path, it can create a loop.
Keep handlers small. Avoid starting complex UI work, showing dialogs, or performing an unbounded retry from a global callback. Use a recursion guard, a timeout, and a bounded queue if reporting requires asynchronous work.
PlatformDispatcher.instance.onError returns a Boolean. Return true only when your code has handled or recorded the error. Returning false allows the platform's default handling to continue. Do not return true merely to hide a failure.
Zone mismatches
runZonedGuarded can catch errors in a Dart zone, but it is not the recommended primary replacement for the Flutter and platform handlers. Zones can also cause confusing behavior when callbacks or futures are created in one zone and invoked in another. A global handler installed through FlutterError.onError and PlatformDispatcher.instance.onError matches Flutter's current error channels more directly.
Use a zone only for a specific need, such as attaching request metadata to logs. Do not assume that wrapping runApp in a zone guarantees that every Flutter error reaches its callback.
> callout warning > Test handlers in profile and release-like builds. A handler that appears correct in debug mode can lose useful output, expose sensitive data, or hide a failure when compiled for production.
Exercise
Exercise
Why should an error reporter guard against failures inside the reporter itself, and when should PlatformDispatcher.instance.onError return true?
Show answer
The reporter can throw or trigger another error, causing recursion or repeated failures, so it needs a guard and bounded behavior. Return true only after the application has handled or recorded the asynchronous error; otherwise return false.