Skip to content

Fix RouteObserver didPushNext not firing when routes are pushed synchronously - #189057

Merged
auto-submit[bot] merged 16 commits into
flutter:masterfrom
Mairramer:fix-routeobserver-sync-push
Nov 19, 2026
Merged

auto-submit[bot] merged 16 commits into
flutter:masterfrom
Mairramer:fix-routeobserver-sync-push

Conversation

@Mairramer

@Mairramer Mairramer commented Jul 7, 2026 •

Copy link
Copy Markdown
Contributor

This PR fixes a bug in RouteObserver where widgets subscribing via RouteAware fail to receive the didPushNext notification when multiple routes are pushed synchronously.

Fixes #189050
Fixes #70272

Pre-launch Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance.

Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. 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.

@github-actions github-actions Bot added framework flutter/packages/flutter repository. See also f: labels. f: routes Navigator, Router, and related APIs. labels Jul 7, 2026
@Mairramer

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request modifies RouteObserver.subscribe to trigger didPushNext on a newly subscribed RouteAware if the route is active but not current, and adds corresponding unit tests. Feedback indicates that triggering didPushNext unconditionally introduces an inconsistency when a route of a different type is on top of the subscribed route, and suggests only triggering it if a route of the observed type is currently on top.

@Mairramer

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces an Expando named _coveredRoutes to RouteObserver to track covered routes and trigger didPushNext upon subscription when appropriate, along with corresponding unit tests. Feedback suggests also updating didRemove and didReplace in RouteObserver to properly manage the state of _coveredRoutes and prevent potential inconsistencies.

@Mairramer

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces _coveredRoutes (an Expando<bool>) to RouteObserver to track which routes are covered by another route, ensuring that RouteAware subscribers receive didPushNext notifications when multiple routes are pushed synchronously. It also adds corresponding unit tests. The review feedback highlights a bug in didRemove where _coveredRoutes[previousRoute] is cleared unconditionally instead of inheriting the coverage status of the removed route, and requests public-quality documentation for the private _coveredRoutes field to comply with the repository style guide.

@Mairramer
Mairramer marked this pull request as ready for review August 23, 2026 20:03

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates RouteObserver to track covered routes using an Expando<bool>, ensuring that RouteAware.didPushNext is correctly triggered when a route is subscribed to while already covered. Corresponding unit tests and mock updates are also added. Feedback suggests adding public-quality documentation (///) to the private _coveredRoutes field to explain its purpose and usage, in accordance with the Flutter style guide.

@Mairramer Mairramer added the CICD Run CI/CD label Sep 7, 2026
@flutter-dashboard flutter-dashboard Bot removed the CICD Run CI/CD label Sep 7, 2026
@Mairramer Mairramer added the CICD Run CI/CD label Sep 7, 2026
@flutter-dashboard flutter-dashboard Bot removed the CICD Run CI/CD label Sep 24, 2026
@Mairramer Mairramer added the CICD Run CI/CD label Sep 26, 2026
@justinmc
justinmc requested a review from hannah-hyj September 29, 2026 22:45
@flutter-dashboard flutter-dashboard Bot removed the CICD Run CI/CD label Oct 8, 2026
@Mairramer Mairramer added the CICD Run CI/CD label Oct 8, 2026
@flutter-dashboard flutter-dashboard Bot removed the CICD Run CI/CD label Nov 2, 2026
@Mairramer Mairramer added the CICD Run CI/CD label Nov 2, 2026
@hannah-hyj

Copy link
Copy Markdown
Member

Hi Mairramer, thank you for your contribution.

Using Expando here is a really good approach!

One thing to watch out for is that storing a boolean (Expando<bool>) can get out of sync with the stack in a few cases:

  1. Multi-route exit ([A, B, C] -> [A], e.g. removing two top pages in declarative Navigator.pages): Navigator notifies didRemove(C, A) (setting _coveredRoutes[A] = null) and then didRemove(B, A), which sets _coveredRoutes[A] = _coveredRoutes[B]. Since _coveredRoutes[B] was still true from when C covered it, A ends up marked as covered even though it is now the top route.
  2. Inserting a route below the top route ([A, B] -> [A, M, B]): Navigator calls didPush(M, A). That marks A as covered, but leaves _coveredRoutes[M] as null even though B is on top of M.
  3. Replacing a covering route with a non-R route (didReplace(newRoute: dialogRoute, oldRoute: B)): didReplace only runs when newRoute is R && oldRoute is R, so if B is replaced by a non-R route, A remains marked as covered by R.

To keep the same Expando design while handling these cleanly, we could store the neighbor R routes (_routeAbove and _routeBelow of type Expando<R>) instead of a bool:

  final Expando<R> _routeAbove = Expando<R>();
  final Expando<R> _routeBelow = Expando<R>();

  void _unlinkRoute(R route) {
    final R? below = _routeBelow[route];
    final R? above = _routeAbove[route];
    if (below != null) {
      _routeAbove[below] = above;
    }
    if (above != null) {
      _routeBelow[above] = below;
    }
    _routeBelow[route] = null;
    _routeAbove[route] = null;
  }

Then:

  • In subscribe: check if (_routeAbove[route] != null)
  • In didPush: when route is R && previousRoute is R, splice route between previousRoute and _routeAbove[previousRoute]
  • In didPop and didRemove: when route is R, call _unlinkRoute(route)
  • In didReplace: if oldRoute is R && newRoute is R, transfer oldRoute's _routeBelow/_routeAbove links to newRoute; else if oldRoute is R, call _unlinkRoute(oldRoute)

@flutter-dashboard flutter-dashboard Bot removed the CICD Run CI/CD label Nov 3, 2026
@Mairramer
Mairramer requested a review from hannah-hyj November 3, 2026 22:50
hannah-hyj
hannah-hyj previously approved these changes Nov 10, 2026
/// of changes to the [Navigator]'s session history.
class RouteObserver<R extends Route<dynamic>> extends NavigatorObserver {
final Map<R, Set<RouteAware>> _listeners = <R, Set<RouteAware>>{};
final Expando<R> _routeAbove = Expando<R>();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add a brief comment on _routeAbove / _routeBelow explaining why Expando is used? For example:

/// Doubly-linked list of active routes of type [R] on the observed [Navigator].
///
/// Uses [Expando] so [RouteObserver] neither retains [Route] instances after
/// they are disposed nor invokes getters on mocked [Route] objects in tests.
final Expando _routeAbove = Expando();
final Expando _routeBelow = Expando();

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

}());
return contained;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we update the doc comment on subscribe to mention the immediate didPush / didPushNext calls

For example:

/// Subscribe [routeAware] to be informed about changes to [route].
///
/// Subscribing immediately calls [RouteAware.didPush] on [routeAware]. If
/// another route of type R is already on top of [route], it also calls
/// [RouteAware.didPushNext]. If [routeAware] is already subscribed to
/// [route], this method does nothing.
///
/// Going forward, [routeAware] will be informed about qualifying changes
/// to [route], e.g. when [route] is covered by another route or when [route]
/// is popped off the [Navigator] stack.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@hannah-hyj

Copy link
Copy Markdown
Member

Thanks for updating the PR and adding the new tests, @Mairramer! LGTM with only doc comments

@hannah-hyj hannah-hyj added the CICD Run CI/CD label Nov 10, 2026
@Mairramer Mairramer added the CICD Run CI/CD label Nov 13, 2026
@Mairramer
Mairramer requested a review from hannah-hyj November 13, 2026 20:28

@hannah-hyj hannah-hyj left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Mairramer Mairramer added the autosubmit Merge PR when tree becomes green via auto submit App label Nov 19, 2026
@auto-submit
auto-submit Bot added this pull request to the merge queue Nov 19, 2026
Merged via the queue into flutter:master with commit ed20d2e Nov 19, 2026
25 checks passed
@flutter-dashboard flutter-dashboard Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Nov 19, 2026
auto-submit Bot pushed a commit to flutter/packages that referenced this pull request Nov 20, 2026
Roll Flutter from e8cc6f3451b7 to b34b017b48ad (36 revisions)

flutter/flutter@e8cc6f3...b34b017

2026-11-20 [email protected] Fix OverflowBox dry layout when deferToChild overrides the constraints (flutter/flutter#196839)
2026-11-20 [email protected] Roll Dart SDK from f118f476ebce to 3b5f45d812f8 (1 revision) (flutter/flutter#197048)
2026-11-20 [email protected] [macOS] Register plugins in multiple_windows example (flutter/flutter#196068)
2026-11-20 [email protected] Roll Skia from 87027a9d4659 to a1edf5c2ea5c (1 revision) (flutter/flutter#197045)
2026-11-20 [email protected] Add highp directives to UberSDF to prevent overflow (flutter/flutter#196997)
2026-11-20 [email protected] [Impeller] Cap the Vulkan command pool recycler cache (flutter/flutter#194793)
2026-11-20 [email protected] Roll Skia from 12610b69511d to 87027a9d4659 (4 revisions) (flutter/flutter#197037)
2026-11-20 [email protected] [macOS] Unregister nil binary message handlers (flutter/flutter#193672)
2026-11-20 [email protected] Roll Dart SDK from b803c3ec01de to f118f476ebce (2 revisions) (flutter/flutter#197034)
2026-11-20 [email protected] Roll Skia from 2f1436ba8e64 to 12610b69511d (3 revisions) (flutter/flutter#197030)
2026-11-20 [email protected] [Android] Do not use the HCPP external view embedder from its platform tasks (flutter/flutter#196490)
2026-11-20 [email protected] Roll Dart SDK from 849e17ad3ae6 to b803c3ec01de (1 revision) (flutter/flutter#197012)
2026-11-20 [email protected] [iOS a11y] Synchronize hit-test children when replacing semantics objects (flutter/flutter#196439)
2026-11-20 [email protected] Sync CHANGELOG.md from stable (flutter/flutter#197019)
2026-11-19 [email protected] [flutter_tools] Reland "Extract Windows archives using native tar" (flutter/flutter#193515)
2026-11-19 [email protected] [Reland][flutter_tools] Migrate lib/runner.dart off runInContext and overrides (flutter/flutter#196931)
2026-11-19 [email protected] Include assigned issues in Android incoming triage link (flutter/flutter#197008)
2026-11-19 [email protected] [Flutter GPU] Record on the UI thread, encode and submit on the raster thread (flutter/flutter#194882)
2026-11-19 [email protected] Add Pixel 7 Pro Impeller benchmarks without fixed-performance mode (flutter/flutter#197010)
2026-11-19 [email protected] Reduce time of uiscene_plugin_lifecycle_test_ios (flutter/flutter#196363)
2026-11-19 [email protected] [native assets] Respect Apple deployment targets (flutter/flutter#194146)
2026-11-19 [email protected] iOS: Represent platform view frames as FlutterPlatformViewFrames (flutter/flutter#196955)
2026-11-19 [email protected] Roll Skia from 0bf961e4ff05 to 2f1436ba8e64 (3 revisions) (flutter/flutter#196999)
2026-11-19 [email protected] [ios] Record OCMReject expectations before the stimulus in FlutterPluginAppLifeCycleDelegateTest (flutter/flutter#193011)
2026-11-19 [email protected] Fix post-monorepo doc rot in docs/infra and dev/bots/README.md (flutter/flutter#193010)
2026-11-19 [email protected] Roll Skia from c3567e170362 to 0bf961e4ff05 (4 revisions) (flutter/flutter#196994)
2026-11-19 [email protected] Close Linux popups when their parent loses focuses (flutter/flutter#196825)
2026-11-19 [email protected] Roll Packages from dca2e58 to 81b7c44 (6 revisions) (flutter/flutter#196992)
2026-11-19 [email protected] Roll Dart SDK from d8d40cf9c21b to 849e17ad3ae6 (1 revision) (flutter/flutter#196986)
2026-11-19 [email protected] Fix RouteObserver didPushNext not firing when routes are pushed synchronously (flutter/flutter#189057)
2026-11-19 [email protected] [Impeller] Reuse render targets on text shadow cache misses (flutter/flutter#194769)
2026-11-19 [email protected] Roll Skia from 81de64af9806 to c3567e170362 (3 revisions) (flutter/flutter#196982)
2026-11-19 [email protected] Reland: "Move macOS desktop builds to ThinLTO" (flutter/flutter#196928)
2026-11-19 [email protected] [Linux] Desynchronize the Wayland subsurface so frames don't redraw the GTK window (flutter/flutter#195287)
2026-11-19 [email protected] Roll Skia from 3dfb69d709c7 to 81de64af9806 (1 revision) (flutter/flutter#196975)
2026-11-19 [email protected] Update `ProxyAnimation.didStartListening` and `ProxyAnimation. didStopListening` to use dart's `Conditional member access` (flutter/flutter#195762)

If this roll has caused a breakage, revert this CL and set the roller
to dry run mode using the controls here:
https://autoroll.skia.org/r/flutter-packages
Please CC [email protected],[email protected] on the revert to ensure that a human
is aware of the problem.

To file a bug in Packages: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CICD Run CI/CD f: routes Navigator, Router, and related APIs. framework flutter/packages/flutter repository. See also f: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RouteAware didPushNext is not called when pushing two routes at once RouteAware didPushNext not called when initial route creates multiple routes

2 participants