Boundary
Boundary is a new widget for Flutter that takes over FlutterError.onError and ErrorWidget.builder to make them composable and scoped.
If you ever wanted to have your error reporting applied only a specific part of your widget tree, or if you found difficult to implement an "Oops"/Loading screen, then this library is for you.
Installation
For Boundary
to work, it is necessary to call setupBoundary
first.
This can be done inside your main
function like so:
For tests purpose, setupBoundary
returns a function to revert the settings
to their default behavior:
Principle
Error reporting and fallback UI are now represented through one universal widget:
Boundary
This widget, when inserted inside the widget tree, is able to catch exceptions
from descendants (and only descendants) to then create a fallback UI.
Here's a typical example:
Which renders the following:
Notice how, even if there's a Container
with padding and a red background
as child of Boundary
, the "Oops" screen doesn't show any of these:
The widget returned by fallbackBuilder
is in an entirely different widget tree.
But the failing subtree (Container -> Builder) is not removed for the tree either!
Its state is preserved and it is simply offstaged, until it rebuilds successfuly.
This is proved by the following example, which shows how Boundary
can be used
to show a loading/error screen from a FutureBuilder
deeper in the widget tree
– without having a reference on the Future
.
FAQ
How to remove the fallback screen
Once an exception is thrown, the fallback screen is shown. But you may want to
stop showing that fallback at some point.
To achieve this, simply rebuild the failling widget such that it doesn't throw
anymore. This will automatically remove the fallback screen.
What happens if there's an exception inside fallbackBuilder
?
If there's an exception inside fallbackBuilder
, then the exception is propagated
to the next Boundary
, until there are none anymore.
Using the previous snippet, this will first print 42
in the console, then
render a Text
with "42" on screen.