[go_router] Add maybePop for safe navigation pops#11927
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
Introduce maybePop on GoRouter, GoRouterDelegate, and GoRouterHelper to mirror Navigator.maybePop and BackButton while calling restore() when a pop completes synchronously. Adds comprehensive tests for maybePop, restore, canPop, and mixed Navigator.pop usage.
There was a problem hiding this comment.
Code Review
This pull request introduces a maybePop method to GoRouter, GoRouterDelegate, and GoRouterHelper to support popping routes without throwing exceptions when the stack is empty, mirroring Navigator.maybePop. It also adds extensive tests covering the new method, state restoration, and integration scenarios. The review feedback suggests adding a state.mounted check within the asynchronous loop in GoRouterDelegate.maybePop to prevent potential exceptions if a navigator is unmounted during execution.
5742cef to
4160755
Compare
Skip NavigatorState instances that are disposed during the async await gap when iterating the navigator stack.
8d95ecb to
4b39b05
Compare
|
Would appreciate a review when you have time. Thanks! |
Summary
maybePoptoGoRouter,GoRouterDelegate, andGoRouterHelper(context.maybePop()), mirroringNavigator.maybePopand MaterialBackButtonbehavior.pop(),maybePop()returnsfalseinstead of throwing when nothing can be popped.maybePop()callsrestore()to keeprouteInformationProviderin sync withGoRouterDelegate.currentConfiguration(same aspop()).maybePop,restore(),canPop, mixedNavigator.pop/context.popflows, and frameworkBackButtonintegration./coverage/ingo_router.gitignore.Motivation
Apps commonly gate back navigation with
if (context.canPop()) context.pop(). Framework widgets likeBackButtonuseNavigator.maybePop(), which is safe but does not sync GoRouter URL/state.maybePop()provides a single GoRouter-native safe pop API:Test plan
flutter test packages/go_router/test/maybe_pop_test.dartflutter test packages/go_router/test/restore_test.dartflutter test packages/go_router/test/extension_test.dartflutter test packages/go_router/test/delegate_test.dartflutter test packages/go_router/test/router_test.dart