r/flutterhelp • • 12d ago

OPEN How to properly implement an error page in Flutter?

I'm aware that ErrorWidget.builder exists, but the downside about this builder is that it only replaces the widget that failed to render not necessarily the entire screen. Is there a way to replace the entire content of the screen with an alternative widget? Or maybe navigate to some sort of error route? How do you guys usually tackle this?

4 Upvotes

10 comments sorted by

1

u/remsbdj 11d ago

what sort of certain errors are you sure about will get displayed to deserve a dedicated error widget, instead of avoiding this error ?

1

u/Beurni 11d ago

Unexpected errors thrown by widgets, like null references for example.

→ More replies (3)

2

u/SifuWong99 10d ago

This sounds like a really bad solution to a problem we have not been told. What is your actual problem?

1

u/syedaalishan 11d ago

ErrorWidget.builder replaces the failed subtree, so it won't automatically give you a whole-screen error boundary. I'd keep that fallback simple and report the original exception through FlutterError.onError. For a full-page retry screen, use an explicit error state owned above the page content. Avoid pushing a new route directly from ErrorWidget.builder while a build is in progress. Flutter's guide explains the different error paths: https://docs.flutter.dev/testing/errors

1

u/Weak-Arrival-7811 11d ago

ErrorWidget.builder only covers the widget that failed, so a full-screen fallback needs a few pieces together:

- FlutterError.onError catches framework errors (build, layout, paint) and PlatformDispatcher.instance.onError catches async errors that would otherwise go unhandled

- in both, log the error, then navigate with a global navigatorKey, e.g. navigatorKey.currentState?.pushNamedAndRemoveUntil('/error', (_) => false). Wrap it in a post-frame callback so you're not navigating in the middle of a build

- keep an ErrorWidget.builder as well, returning something neutral instead of the grey box, because the frame that failed still renders before you navigate

One thing to guard against: if the error page itself throws, you'll loop. Keep it free of providers and network calls.

1

u/chemtrail-organics 7d ago

Usually we use a red banner for serious errors or a toaster message for transient errors. Your page should have an error state where it shows one of these, don't replace it with another page.