Repository navigation
Conversation
Browser back navigation removed the page while the Navigator was still building, and the synchronous notifyListeners() from onDidRemovePage triggered setState() during build. Fixes Stacked-Org#1184 Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Collaborator
Author
|
Superseded by #1198, which is the same change opened from a branch in this repository instead of a fork so that CI can post the coverage report. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #1184: browser back navigation on a Stacked web app (default
stacked create app -t webtemplate) throwssetState() or markNeedsBuild() called during buildfrom_RootRouter.Root cause
Matched against the reported stack trace:
Navigator.onDidRemovePageinroute_navigator.dartcallsrouter.removeRoute(routeData).StackRouter._removeRouteinrouting_controller.dartcallednotifyAll(forceUrlRebuild: true)unconditionally, even when the page was already gone from_pages(index-1).notifyAlltriggers_RootRouterState._handleRebuild, which callssetState().When the removal is a side effect of the Navigator updating while the router widget's own rebuild is still cascading down into a descendant, that synchronous
setState()lands during build and throws.Fix
lib/src/router/controller/routing_controller.dart,_removeRoute:_pages, return early. Nothing changed, so nothing is notified. This is a deliberate behaviour change: a redundantremoveRouteused to still trigger a full rebuild and URL update, now it is a no-op.SchedulerBinding.instance.schedulerPhase == SchedulerPhase.persistentCallbacks, the notification is deferred withaddPostFrameCallback. In every other phase it stays synchronous, exactly as before, so normal navigation timing is unchanged.The guard sits at the notification point rather than in the
onDidRemovePagecallback so it also covers the second removal path introduced by #1193 (StackedPage.poppedeagerly removing the page). Whichever path removes the page first, the other becomes a safe no-op.Tests
test/router/browser_back_navigation_test.dart(new):masterby removing the top page from aListenableBuilderinside that page while_RootRouteris rebuilding, then asserts no exception with the fix.handlePopRoute()asserting no exception across several frames.Note: on Flutter 3.41 a plain browser or system back alone did not reproduce the crash (the reporter was on 3.35.4), so the first test forces the race directly and is the reliable regression guard.
Verification
flutter test: 95 passed, 1 pre-existing skipdart analyze lib test: no new issues🤖 Generated with Claude Code