Sub topic 1
Two Kinds of Uncaught Errors: Framework Errors and Async Errors
Prerequisites
This chapter assumes that you can build a simple Flutter app, use runApp, and read basic Dart code. You should also know how try/catch, Future, and async/await work.
Flutter has two main global error channels. The channel used depends on how the error reaches the Flutter application.
FlutterError.onErrorreceives errors caught by the Flutter framework.PlatformDispatcher.instance.onErrorreceives uncaught errors that escape the framework, including many asynchronous errors.
These handlers are global callbacks. You install them near the start of main() so they are active before the application creates its widgets.
How Flutter routes uncaught errors
A framework error happens while Flutter is performing work such as building, laying out, painting, or handling a gesture callback. Flutter catches the exception and reports it through FlutterError.onError.
An asynchronous error usually happens later, after the current callback has returned. For example, a Future may fail after a delay. If no code catches that failure, it can reach PlatformDispatcher.instance.onError.
| Error source | Typical example | Main handler |
|---|---|---|
| Framework work | A widget's `build` method throws | `FlutterError.onError` |
| Uncaught asynchronous work | An unhandled failing `Future` | `PlatformDispatcher.instance.onError` |
The distinction is about the path the error takes, not only whether the original code contains async.
For example, this button throws during a framework-invoked callback:
ElevatedButton(
onPressed: () {
throw StateError('Synchronous callback failure');
},
child: const Text('Trigger framework error'),
)The framework invokes onPressed, catches the synchronous exception, and reports it through FlutterError.onError.
This button creates an asynchronous failure:
ElevatedButton(
onPressed: () async {
await Future<void>.delayed(const Duration(milliseconds: 100));
throw StateError('Failure after the callback returned');
},
child: const Text('Trigger async error'),
)The callback returns a Future, but the framework does not wait for that future in the normal VoidCallback path. The later exception is therefore unhandled unless the callback catches it. It can reach PlatformDispatcher.instance.onError.
Install both handlers
Initialize the Flutter binding before installing handlers. WidgetsFlutterBinding.ensureInitialized() creates the connection between the Dart code and Flutter's engine. It is also safe to call when the binding has already been initialized.
import 'dart:ui';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
FlutterError.onError = (FlutterErrorDetails details) {
FlutterError.presentError(details);
debugPrint('Framework error: ${details.exceptionAsString()}');
};
PlatformDispatcher.instance.onError = (Object error, StackTrace stack) {
debugPrint('Uncaught async error: $error');
debugPrint('$stack');
return true;
};
runApp(const MaterialApp(
home: Scaffold(
body: Center(child: Text('Error handlers installed')),
),
));
}FlutterErrorDetails contains more than the exception. It can include the stack trace, the library that reported the problem, and context about the framework operation. Preserve this information when forwarding the error to a logging or crash-reporting service.
FlutterError.presentError(details) keeps Flutter's normal diagnostic output. If you replace FlutterError.onError without calling it, useful console output may disappear during development.
The asynchronous handler has a different signature. It receives the error and a StackTrace separately. Its Boolean result tells the platform dispatcher whether the error was handled. Return true after recording the error. Return false if your handler did not handle it and you want the platform's fallback behavior.
Check the source before debugging
When an error appears, first identify whether it occurred during framework work or after an asynchronous boundary. Then inspect the corresponding handler. A try/catch around the code may prevent either global handler from running, which is usually the desired result when the failure is expected and recoverable.
Exercise
A widget's build method throws immediately, while a Future started by a button fails after a delay. Which global handlers should you inspect first?
Show answer
Inspect FlutterError.onError for the build failure and PlatformDispatcher.instance.onError for the uncaught delayed Future failure.