Sub topic 4
Reading FlutterErrorDetails and Keeping Console Output with presentError
Assume you know basic Dart, async and await, Flutter widgets, runApp, and try/catch. This chapter focuses on the information Flutter gives you when it reports a framework error.
What FlutterErrorDetails contains
When Flutter catches an error during building, layout, painting, or a synchronous gesture callback, it creates a FlutterErrorDetails object. Flutter passes that object to FlutterError.onError.
The object contains more than the exception message. It also describes where and how Flutter found the problem.
| Property or method | What it provides |
|---|---|
| `exception` | The original exception or error object |
| `stack` | The stack trace, when one is available |
| `library` | The Flutter library that reported the error |
| `context` | A diagnostic description of the operation in progress |
| `exceptionAsString()` | A readable representation of the exception |
| `informationCollector` | Extra diagnostic information collected lazily |
The context value is a diagnostic node rather than an ordinary string. It may describe an operation such as “building” or “performing layout.” The library value can help separate errors reported by Flutter from errors reported by your own code.
Keep Flutter's normal console output
Replacing FlutterError.onError replaces the default handler. If your replacement only sends the exception to a service, you can lose Flutter's detailed console diagnostics during development.
Call FlutterError.presentError(details) inside your handler to preserve that output. Then add your own reporting logic.
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
FlutterError.onError = (FlutterErrorDetails details) {
// Preserve Flutter's normal diagnostic output.
FlutterError.presentError(details);
// Add application-specific logging or crash reporting.
ErrorReporter.report(details);
};
runApp(const ErrorDetailsApp());
}
class ErrorReporter {
static void report(FlutterErrorDetails details) {
debugPrint('library: ${details.library}');
debugPrint('exception: ${details.exceptionAsString()}');
debugPrint('stack: ${details.stack}');
debugPrint('context: ${details.context}');
}
}
class ErrorDetailsApp extends StatelessWidget {
const ErrorDetailsApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Error details')),
body: Center(
child: ElevatedButton(
onPressed: () {
throw StateError('The button was pressed incorrectly');
},
child: const Text('Trigger framework error'),
),
),
),
);
}
}The call to presentError does not fix the exception or prevent the error from being reported. It asks Flutter to format and display its usual diagnostic message. Your reporter can then extract structured fields for a logging or crash-reporting service.
Understand the reporting flow
The handler receives one FlutterErrorDetails value for a framework error. The value travels through the default presentation step and your application-specific reporting step.
Avoid common reporting mistakes
Do not assume that exception.toString() contains all useful information. Prefer exceptionAsString() for the formatted exception text, and send the stack trace separately.
Do not print sensitive data from diagnostic fields. Widget diagnostics can contain values from the current screen. Filter or redact data before sending it outside the device.
Do not throw another exception from the handler. A failure in the reporting code can hide the original problem or create repeated error handling. Keep the handler small, and make the reporting path tolerant of missing stack traces.
The default presentation is especially useful during development. In a production build, your logging service should provide the durable record because console output may not be available after the app exits.
Exercise
Exercise
Why should a custom FlutterError.onError handler call FlutterError.presentError(details) before sending details to a reporting service?
Show answer
The custom handler replaces Flutter's default handler. presentError preserves Flutter's normal formatted console diagnostics while the reporting service receives the structured error data.