Bored of seeing the default red or grey screen when something goes wrong in your Flutter app?
What's the Default?
Flutter shows a red screen (debug mode) or a grey screen (production mode) by default when there's a rendering error.
Override the Default
You can change this behavior.
Stop Showing the Default Screen
To stop Flutter from showing the default red/grey screen, you can remove the presentError
function call in FlutterError.onError
.
Using ErrorWidget.builder
A common way is to override the default error widget using ErrorWidget.builder
in your MaterialApp
. This lets you show your own widget instead of the default error screen.
You can create a simple text message or a more complex UI. The code from the image shows how to make a basic text widget display the error summary.
Other Options
Instead of just showing an error widget, you could also:
- Redirect the user to a safe screen.
- Show a small message like a "toast" notification.
This gives users a better experience when errors happen.