Sub topic 9
Replacing the Red Error Screen with `ErrorWidget.builder`
This chapter assumes that you can build a basic Flutter app and understand Dart exceptions, try/catch, and Future values. It focuses on errors thrown while Flutter builds, lays out, or paints a widget.
What `ErrorWidget.builder` does
When a widget's build method throws, Flutter catches the framework error. Flutter then replaces the failed widget with an ErrorWidget.
In debug mode, the default widget is usually a red error screen. That screen is useful during development because it shows diagnostic information. It is not suitable for most users in a release build.
ErrorWidget.builder is a global callback that lets you choose the replacement widget:
ErrorWidget.builder = (FlutterErrorDetails details) {
return const Material(
child: Center(
child: Text('Something went wrong on this screen.'),
),
);
};The callback receives a FlutterErrorDetails object. That object contains the exception, stack trace, and context about the failure. Use it for logging if needed, but avoid displaying raw exception details to users.
Install the builder before runApp() so it applies from the start of the application.
A complete example
The following app deliberately throws from BrokenWidget.build. Flutter reports the framework error and displays the custom replacement widget in that location.
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()}');
};
ErrorWidget.builder = (FlutterErrorDetails details) {
return const Material(
color: Colors.white,
child: Center(
child: Padding(
padding: EdgeInsets.all(24),
child: Text(
'This part of the screen is unavailable.',
textAlign: TextAlign.center,
),
),
),
);
};
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Error widget example')),
body: const Center(
child: BrokenWidget(),
),
),
);
}
}
class BrokenWidget extends StatelessWidget {
const BrokenWidget({super.key});
@override
Widget build(BuildContext context) {
throw StateError('The widget cannot be built');
}
}FlutterError.onError and ErrorWidget.builder have different jobs:
FlutterError.onErrorreceives error information for console output, logging, or crash reporting.ErrorWidget.builderdecides what users see where the failed widget would have appeared.
Calling FlutterError.presentError keeps Flutter's normal diagnostic output. A custom builder does not replace error reporting. It only changes the visual fallback.
Limits and design choices
ErrorWidget.builder is not a general exception handler. It does not replace PlatformDispatcher.instance.onError for uncaught asynchronous errors. For example, a failed Future that nobody awaits does not automatically create an ErrorWidget.
The replacement widget should be simple and unlikely to fail. Avoid starting network requests, navigating, or performing complicated state updates inside it. If the replacement widget throws too, Flutter may produce another error while handling the first one.
A global fallback also cannot always know which screen failed. For a more useful experience, handle expected failures near the feature that owns them. Use ErrorWidget.builder as a final UI fallback for unexpected framework errors.
Exercise
What is the difference between FlutterError.onError and ErrorWidget.builder?
Show answer
FlutterError.onError receives framework error details for reporting, while ErrorWidget.builder creates the replacement widget shown where a failed widget was built.