Repository navigation
Conversation
Scoping a branch location removes the URI of routes on ancestor navigators, but the branch Navigator is still built from the unscoped match list. Popping such a route left the scoped location unchanged, so the Navigator was not rebuilt and GoRouterState.of in branch pages kept the URI, path parameters and top route of the removed route. Returning to the branch with goBranch had the same result. Rebuild the Navigator when the URI it was built from changes. Pushing a route does not change that URI, so branch pages are still not rebuilt in that case. Removing a route on an ancestor navigator also left a redirect-only parent nested in the preceding ShellRouteMatch, which failed the redirect-only assertions after pop and goBranch. Remove trailing redirect-only routes from that shell as well.
- Remove redirect-only routes before a shell route that remove() empties. - Build the path in remove() from the encoded matchedLocation so pop() keeps encoded delimiters such as %2F. - Rebuild a branch navigator when the top match it was built from is gone. - Use the standard license header in the new test file.
|
Hi @AyatAliev, thank you for your contribution! This pull request has been converted to a draft because it does not appear to follow our pull request template:
How to fix this
Click here for the raw Markdown of the Pre-Review Checklist to copy and paste## Pre-Review Checklist
- [ ] I read the [Contributor Guide] and followed the process outlined there for submitting PRs.
- [ ] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools.
- [ ] I read the [Tree Hygiene] page, which explains my responsibilities.
- [ ] I read and followed the [relevant style guides] and ran [the auto-formatter].
- [ ] I signed the [CLA].
- [ ] The title of the PR starts with the name of the package surrounded by square brackets, e.g. `[shared_preferences]`
- [ ] I [linked to at least one issue that this PR fixes] in the description above.
- [ ] I followed [the version and CHANGELOG instructions], using [semantic versioning] and the [repository CHANGELOG style], or I have commented below to indicate which documented exception this PR falls under[^1].
- [ ] I updated/added any relevant documentation (doc comments with `///`).
- [ ] I added new tests to check the change I am making, or I have commented below to indicate which [test exemption] this PR falls under[^1].
- [ ] All existing and new tests are passing.
If you need help, consider asking for advice on the #hackers-new channel on [Discord].
**Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.
[^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
<!-- Links -->
[Contributor Guide]: https://github.com/flutter/packages/blob/main/CONTRIBUTING.md
[AI contribution guidelines]: https://github.com/flutter/flutter/blob/master/CONTRIBUTING.ai.md#pull-requests
[Tree Hygiene]: https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md
[relevant style guides]: https://github.com/flutter/packages/blob/main/CONTRIBUTING.md#style
[the auto-formatter]: https://github.com/flutter/packages/blob/main/script/tool/README.md#format-code
[CLA]: https://cla.developers.google.com/
[Discord]: https://github.com/flutter/flutter/blob/master/docs/contributing/Chat.md
[linked to at least one issue that this PR fixes]: https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md#overview
[the version and CHANGELOG instructions]: https://github.com/flutter/flutter/blob/master/docs/ecosystem/contributing/README.md#version-and-changelog-updates
[semantic versioning]: https://dart.dev/tools/pub/versioning#semantic-versions
[repository CHANGELOG style]: https://github.com/flutter/flutter/blob/master/docs/ecosystem/contributing/README.md#changelog-style
[test exemption]: https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md#tests |
There was a problem hiding this comment.
Code Review
This pull request resolves several routing and state restoration issues in go_router. It updates RouteMatchList to preserve encoded path delimiters and refactors the removal of trailing redirect-only routes, including those nested within shell routes. In StatefulNavigationShellState, the scoping of match lists is updated to correctly identify matches after the shell route, and _StatefulShellBranchState is enhanced to track the match list from which the navigator was built to detect when a navigator is outdated. Additionally, regression tests and changelog entries are added. The review feedback suggests resolving a potential issue where _removeTrailingRedirectOnlyMatches could return an invalid empty ShellRouteMatch, and correcting an invalid import of package:material_ui/material_ui.dart in the test file.
There was a problem hiding this comment.
Code Review
This pull request fixes several issues in go_router related to StatefulShellRoute branch restoration, location metadata, and redirect-only route cleanup. Specifically, it updates RouteMatchList to preserve encoded path delimiters, refactors trailing redirect-only route removal, and improves navigator rebuilding logic in StatefulNavigationShellState by tracking the match list from which the navigator was built. Review feedback highlights an incorrect material import in the new test file and identifies a logic bug in _removeTrailingRedirectOnlyMatches where empty shell routes are not fully cleaned up.
The shared test router now uses GoRouter.routingConfig so a test can replace its routes while a root route is pushed.
This continues #13202, which GitHub closed after I accidentally deleted its head fork. It has the same commits plus the ones that address the latest review there; the earlier discussion is in #13202.
When a
StatefulShellRoutebranch redirects to a child on the root navigator, returning to the branch withgoBranch()can restore matches that exclude the child while their URI still points to it. The restore parser then keeps that inconsistent cached list, so the redirect page does not appear on re-entry.Find the first match outside the current shell through its ancestor shells and remove that match and the following suffix with
RouteMatchList.remove(). This removes obsolete path parameters and retains query parameters, fragments,extra, and branch imperative pages.RouteMatchList.remove()now takes the new URI path from the encodedmatchedLocationof the last remaining declarative match instead of rebuilding it from decoded path parameters, so encoded delimiters such as%2Fsurvive both branch scoping and a plainpop(). It also removes redirect-only routes left at the end after the removal: at the end of a precedingShellRouteMatch, and before aShellRouteMatchthat becomes empty. This fixes the redirect-only assertion failures onpop()andgoBranch()for a root route nested under a redirect-only route in a branch. In that setup, and for a shell route nested under a redirect-only route,pushReplacement()andreplace()also no longer leave the redirect-only route in the location. Thepop(),pushReplacement()andreplace()failures also exist onmain, independently of branch restoration.Branch navigators are still built from the unscoped match list, so a branch navigator is also rebuilt when the URI it was built from changes, or when the top match it was built from is gone, such as a root route that was pushed before
restartAndRestoreor aRoutingConfigupdate and then popped. This keepsGoRouterState.ofcorrect in branch pages after popping a root route or returning withgoBranch(). Pushing a route onto the root navigator changes neither, so branch pages are still not rebuilt in that case.Fixes flutter/flutter#194209.
Adds fifteen widget tests in
test/branch_restoration_uri_test.dartcovering repeated root redirects, cached URI/path parameters, encoded path delimiters on branch scoping and onpop(), imperative stacks, scoping through both enclosing and root navigators,restartAndRestore,GoRouterState.ofin branch pages, including after popping a root route pushed beforerestartAndRestoreor aRoutingConfigupdate, branch rebuilds on push, and redirect-only parents. For a shell route nested under a redirect-only route, adds aRouteMatchList.removeunit test intest/match_test.dartand apushReplacementtest intest/go_router_test.dart. This package uses batched releases, so instead of a version bump the change addspending_changelogs/fix_cached_branch_uri.yaml, which requests a patch release.Validation
On this branch merged with
mainat76015c1493(go_router18.1.4), Flutter 3.44.9 / Dart 3.12.2:test/branch_restoration_uri_test.dart, the newtest/match_test.darttest and the newtest/go_router_test.darttest fail on unmodifiedmain. The other 3 guard behavior that already works there. All pass with this change.flutter test --no-pub --reporter failures-only: 499 passed.flutter test --no-pub --reporter failures-onlyinexample/: 17 passed.flutter test --no-pub --platform=chrome test/branch_restoration_uri_test.dart test/match_test.dart test/matching_test.dart: 26 passed.flutter analyze --no-pub --fatal-infos: no issues.dart format --output=none --set-exit-if-changedon the changed Dart files: no changes required.flutter_plugin_toolslicense-check,format,analyze,validate --check-for-missing-changesandpublish-checkforgo_router: no issues.Pre-Review Checklist
[shared_preferences]///).If you need help, consider asking for advice on the #hackers-new channel on Discord.
Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the
gemini-code-assistbot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.Footnotes
Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling. ↩ ↩2