New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[go_router] canPop() throws "BadState: No element" on error pages (and maybe other pages) #147465
Comments
@quaaantumdev |
@darshankawar we created a sample app showing the issue: https://github.com/quaaantumdev/gorouter_canpop_issue HomePage works as expected: Pressing the button tries to navigate to a route that does not exist, resulting in the CustomErrorPage. This Error Page utilizes the same CustomPageHeaderSliver, which checks And here is your error log from the
|
Thanks for the update. Seeing the same behavior as reported using the code repo provided. stable, master flutter doctor -v
|
Steps to reproduce
errorBuilder
for GoRouter with some CustomErrorPage() widget.CustomErrorPage()
by accessingGoRouter.of(context).canPop()
inside thebuild(..)
function of yourCustomErrorPage
Expected results
Expecting
GoRouter.of(context).canPop()
to return false if there is not even a single page to pop.Actual results
Accessing
canPop()
will crash withBad state: No element
.One may also trigger this error in other ways, that is just the way I discovered it. I wanted to use my default Header on my error page, which internally checks if it should display a back button, resulting in this error. I guess this is a pretty common use case.
The Problem seems to be, that GoRouter.canPop delegates it to routerDelegate.canPop() which then tries to get the "last" match by accessing
currentConfiguration.matches.last
. This throws as there are no matches. The implementation ofNavigator.canPop()
returns false if there is no route, so i guess it should be changed tocurrentConfiguration.matches.lastOrNull
and return false if no route was found. But whatever value it may return from your point of view, throwing (by accident, i'd say) seems really wrong.Code sample
Initialize GoRouter
Screenshots or Video
No response
Logs
No response
Flutter Doctor output
Doctor output
The text was updated successfully, but these errors were encountered: