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,
to their default behavior:
setupBoundary
returns a function to revert the settingsto 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.
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
as child of
Container
with padding and a red backgroundas 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.
Its state is preserved and it is simply offstaged, until it rebuilds successfuly.
This is proved by the following example, which shows how
to show a loading/error screen from a
– without having a reference on the
Boundary
can be usedto show a loading/error screen from a
FutureBuilder
deeper in the widget tree– without having a reference on the
Future
.