Open quaaantumdev opened 2 weeks ago
@quaaantumdev Can you provide complete runnable code sample that we can directly copy paste and run to verify this further ? Also provide the entire error log.
@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 GoRouter.of(context.canPop()
, which will therefore fail. We catch this exception in the example to show it in the ui:
And here is your error log from the CustomErrorPage
--> CustomPageHeaderSliver.build()
--> GoRouter.of(context).canPop()
Bad state: No element
#0 _Array.last (dart:core-patch/array.dart:56:5)
#1 GoRouterDelegate.canPop (package:go_router/src/delegate.dart:89:58)
#2 GoRouter.canPop (package:go_router/src/router.dart:321:35)
#3 CustomPageHeaderSliver.build (package:gorouter_canpop_issue/main.dart:115:37)
#4 StatelessElement.build (package:flutter/src/widgets/framework.dart:5550:49)
#5 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5480:15)
#6 Element.rebuild (package:flutter/src/widgets/framework.dart:5196:7)
#7 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:5462:5)
#8 ComponentElement.mount (package:flutter/src/widgets/framework.dart:5456:5)
#9 Element.inflateWidget (package:flutter/src/widgets/framework.dart:4335:16)
#10 MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6893:36)
#11 MultiChildRenderObjectElement.mount (package:flutter/src/widgets/fra
Thanks for the update. Seeing the same behavior as reported using the code repo provided.
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
```dart // MaterialApp.router(... GoRouter( // ... your config errorBuilder: (context, state) => CustomErrorPage( routerState: state, ), ) /// a custom error page class CustomErrorPage extends StatelessWidget { final GoRouterState routerState; const GoErrorPage({ super.key, required this.routerState, }); @override Widget build(BuildContext context) { final canPop = GoRouter.of(context).canPop(); // <-- this will crash // show a back button if one can pop ... return Text("Hello there"); } } ```Screenshots or Video
No response
Logs
No response
Flutter Doctor output
Doctor output
```console Doctor summary (to see all details, run flutter doctor -v): [√] Flutter (Channel stable, 3.19.6, on Microsoft Windows [Version 10.0.19045.4291], locale en-GB) [√] Windows Version (Installed version of Windows is version 10 or higher) [√] Android toolchain - develop for Android devices (Android SDK version 34.0.0) [√] Chrome - develop for the web [√] Visual Studio - develop Windows apps (Visual Studio Build Tools 2019 16.11.32) [√] Android Studio (version 2023.1) [√] VS Code (version 1.88.0) [√] Connected device (4 available) [√] Network resources • No issues found! ```