Sub topic 7
Sending Flutter Errors to a Logging or Crash-Reporting Service
This chapter assumes that you know Dart async and await, Flutter widgets, runApp, and basic try/catch. It focuses on forwarding errors after Flutter has delivered them to FlutterError.onError or PlatformDispatcher.instance.onError.
A reporting service records an error outside the running app. It might store the exception, stack trace, app version, device information, and user session identifier. During development, debugPrint is enough to inspect the data. In production, replace those calls with your logging or crash-reporting SDK.
Keep the two error payloads distinct
The framework handler receives a FlutterErrorDetails object. It contains more than the exception itself, including the library that reported it, a human-readable context, and often a stack trace.
The platform handler receives an Object and a StackTrace. Its callback must return a Boolean value:
- Return
truewhen your code handled and reported the error. - Return
falsewhen the error was not handled and should continue through the platform's error path.
Use one reporter class to keep the installation code small and make it easy to replace the development logger later.
| Error channel | Data received | Important action |
|---|---|---|
| `FlutterError.onError` | `FlutterErrorDetails` | Preserve useful details and call `presentError` |
| `PlatformDispatcher.instance.onError` | Error and `StackTrace` | Report both values and return `true` only when handled |
import 'dart:ui';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
class ErrorReporter {
static void reportFlutterError(FlutterErrorDetails details) {
// Replace these calls with your crash-reporting SDK.
debugPrint('Flutter library: ${details.library}');
debugPrint('Exception: ${details.exception}');
debugPrint('Context: ${details.context}');
debugPrint('Stack trace: ${details.stack}');
}
static bool reportAsyncError(Object error, StackTrace stack) {
// Replace these calls with your crash-reporting SDK.
debugPrint('Async error: $error');
debugPrint('Stack trace: $stack');
// The error was recorded, so mark it as handled.
return true;
}
}
void installErrorHandlers() {
FlutterError.onError = (FlutterErrorDetails details) {
// Keep Flutter's normal console and debug output.
FlutterError.presentError(details);
ErrorReporter.reportFlutterError(details);
};
PlatformDispatcher.instance.onError = ErrorReporter.reportAsyncError;
}
void main() {
WidgetsFlutterBinding.ensureInitialized();
installErrorHandlers();
runApp(const MaterialApp(home: Text('Ready')));
}FlutterError.presentError is important during development. Without it, assigning FlutterError.onError replaces the default presentation behavior. Your custom reporter might run while the familiar Flutter error output disappears.
Send safe and useful metadata
A real SDK usually accepts an exception, stack trace, and extra metadata. Add stable context such as the screen name, app version, build mode, and a non-sensitive account identifier. Do not include passwords, access tokens, private message text, or full request headers.
Use the stack trace supplied by Flutter. Creating a new stack trace inside the handler mostly describes the handler itself, not the original failure. If the service distinguishes handled errors from fatal errors, label these events consistently so dashboards do not treat every reported framework error as an application crash.
Understand the reporting flow
Test the integration deliberately
Trigger both paths in a development build. A synchronous exception in a widget callback commonly reaches FlutterError.onError. An unobserved failing Future can reach PlatformDispatcher.instance.onError.
Check the service or console for:
- The original exception message.
- A stack trace pointing to the failure.
- The expected error channel or event type.
- Metadata that does not contain secrets.
- No second exception caused by the reporter.
These global handlers do not replace local recovery. Use try/catch when a screen can recover, such as retrying a request or showing an inline validation message. Use global reporting as a final observation path for errors that escaped normal handling.
Exercise
Why should a Flutter error handler call FlutterError.presentError before forwarding FlutterErrorDetails to a service?
Show answer
Assigning FlutterError.onError replaces Flutter's default error presentation. Calling presentError preserves the normal console and debug output while the custom reporter records the same failure.