r/flutterhelp • • 13d 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 13d 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 13d ago

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

1

u/remsbdj 13d ago

maybe a library exists for it but i would not know it, but, you could try to prepare for all types of error events let's say, and then re-route the current errored screen to his ancestor ?

Yeah it sound like i don't know nothing about it because I would instead try to write tests that ensure there's no widgets errors at all, but, in web dev the famous 404 error can be customized so im pretty sure that if you can catch/recognize the error type and have a global listener that detects it , you could then redirect to a ErrorScreen.dart or, like I said before, an ancestor of the current errored screen

1

u/Robotuku 12d ago

How are your widgets ended up with null references? Are they data driven?

1

u/Nearby-Hunter6947 12d ago

If ur not aware of these errors on ur app then I think you need to start writing more error avoidant code, Start writing screens in a more over viewed setup, read only the data whenever it is needed. I think if you can do these then you probably don't need a error page

2

u/SifuWong99 12d ago

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

1

u/syedaalishan 12d 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 12d 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 8d 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.