Repository navigation
fix(router): avoid notifying listeners during build on page removal #1198
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 2 commits
eb0055b
c50792b
04243b5
5947768
26e2546
111f696
f1a5b64
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ import 'dart:async'; | |
| import 'package:collection/collection.dart'; | ||
| import 'package:flutter/foundation.dart'; | ||
| import 'package:flutter/material.dart'; | ||
| import 'package:flutter/scheduler.dart'; | ||
| import 'package:stacked/src/code_generation/router_annotation/parameters.dart'; | ||
| import 'package:stacked/src/router/controller/controller_scope.dart'; | ||
| import 'package:stacked/src/router/controller/navigation_history/navigation_history_base.dart'; | ||
|
|
@@ -868,9 +869,11 @@ abstract class StackRouter extends RoutingController { | |
|
|
||
| void _removeRoute(RouteMatch route, {bool notify = true}) { | ||
| var pageIndex = _pages.lastIndexWhere((p) => p.routeKey == route.key); | ||
| if (pageIndex != -1) { | ||
| _pages.removeAt(pageIndex); | ||
| if (pageIndex == -1) { | ||
| // Already removed by another path; nothing to notify. | ||
| return; | ||
| } | ||
| _pages.removeAt(pageIndex); | ||
|
|
||
| final stack = _pages.map((e) => e.routeData._match); | ||
| for (final guard in route.guards.whereType<RedirectGuard>()) { | ||
|
|
@@ -881,6 +884,19 @@ abstract class StackRouter extends RoutingController { | |
| _updateSharedPathData(includeAncestors: true); | ||
| _removeTopRouterOf(route.key); | ||
| if (notify) { | ||
| _notifyRouteRemoved(); | ||
| } | ||
| } | ||
|
|
||
| // Defers the notification when called mid-build to avoid setState during | ||
| // build. | ||
| void _notifyRouteRemoved() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [medium] The build-phase guard is applied only to
I verified this on this branch: a descendant widget whose Note this also shows the second setState source ( Since the helper already exists, consider making it a general |
||
| if (SchedulerBinding.instance.schedulerPhase == | ||
| SchedulerPhase.persistentCallbacks) { | ||
| SchedulerBinding.instance.addPostFrameCallback((_) { | ||
| notifyAll(forceUrlRebuild: true); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The deferred The post-frame closure holds onto Scenario: a nested router gets Suggestion: track disposal (for example a |
||
| }); | ||
| } else { | ||
| notifyAll(forceUrlRebuild: true); | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,145 @@ | ||||||||||||||||||||||||||||||||||
| import 'dart:async'; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| import 'package:flutter/foundation.dart'; | ||||||||||||||||||||||||||||||||||
| import 'package:flutter/material.dart'; | ||||||||||||||||||||||||||||||||||
| import 'package:flutter_test/flutter_test.dart'; | ||||||||||||||||||||||||||||||||||
| import 'package:stacked/stacked.dart'; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| class _HomeScreen extends StatelessWidget { | ||||||||||||||||||||||||||||||||||
| const _HomeScreen(); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| @override | ||||||||||||||||||||||||||||||||||
| Widget build(BuildContext context) => const Scaffold(body: Text('Home')); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| class _SecondScreen extends StatelessWidget { | ||||||||||||||||||||||||||||||||||
| const _SecondScreen({required this.router, required this.triggerRemoval}); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| final StackRouter router; | ||||||||||||||||||||||||||||||||||
| final ValueListenable<bool> triggerRemoval; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| @override | ||||||||||||||||||||||||||||||||||
| Widget build(BuildContext context) { | ||||||||||||||||||||||||||||||||||
| return ListenableBuilder( | ||||||||||||||||||||||||||||||||||
| listenable: Listenable.merge([router, triggerRemoval]), | ||||||||||||||||||||||||||||||||||
| builder: (context, _) { | ||||||||||||||||||||||||||||||||||
| if (triggerRemoval.value) { | ||||||||||||||||||||||||||||||||||
| final topPage = router.stack.last; | ||||||||||||||||||||||||||||||||||
| if (topPage.routeKey == const ValueKey('SecondRoute')) { | ||||||||||||||||||||||||||||||||||
| router.removeRoute(topPage.routeData); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| return const Scaffold(body: Text('Second')); | ||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| class _TestRouter extends RootStackRouter { | ||||||||||||||||||||||||||||||||||
| _TestRouter(this.triggerRemoval); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| final ValueListenable<bool> triggerRemoval; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| @override | ||||||||||||||||||||||||||||||||||
| late final Map<String, PageFactory> pagesMap = { | ||||||||||||||||||||||||||||||||||
| 'HomeRoute': (data) => MaterialPageX( | ||||||||||||||||||||||||||||||||||
| routeData: data, | ||||||||||||||||||||||||||||||||||
| child: const _HomeScreen(), | ||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||
| 'SecondRoute': (data) => MaterialPageX( | ||||||||||||||||||||||||||||||||||
| routeData: data, | ||||||||||||||||||||||||||||||||||
| child: _SecondScreen(router: this, triggerRemoval: triggerRemoval), | ||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| @override | ||||||||||||||||||||||||||||||||||
| List<RouteConfig> get routes => [ | ||||||||||||||||||||||||||||||||||
| RouteConfig('HomeRoute', path: '/', fullMatch: true), | ||||||||||||||||||||||||||||||||||
| RouteConfig('SecondRoute', path: '/second', fullMatch: true), | ||||||||||||||||||||||||||||||||||
| ]; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| void main() { | ||||||||||||||||||||||||||||||||||
| testWidgets( | ||||||||||||||||||||||||||||||||||
| 'removing the current top page from a descendant rebuilt while the ' | ||||||||||||||||||||||||||||||||||
| 'router widget is itself rebuilding does not throw "setState() called ' | ||||||||||||||||||||||||||||||||||
| 'during build"', | ||||||||||||||||||||||||||||||||||
| (tester) async { | ||||||||||||||||||||||||||||||||||
| final triggerRemoval = ValueNotifier<bool>(false); | ||||||||||||||||||||||||||||||||||
| final router = _TestRouter(triggerRemoval); | ||||||||||||||||||||||||||||||||||
| addTearDown(triggerRemoval.dispose); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| await tester.pumpWidget( | ||||||||||||||||||||||||||||||||||
| MaterialApp.router( | ||||||||||||||||||||||||||||||||||
| routerDelegate: router.delegate( | ||||||||||||||||||||||||||||||||||
| initialRoutes: [ | ||||||||||||||||||||||||||||||||||
| const PageRouteInfo('HomeRoute', path: '/'), | ||||||||||||||||||||||||||||||||||
| ], | ||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||
| routeInformationParser: router.defaultRouteParser(), | ||||||||||||||||||||||||||||||||||
| routeInformationProvider: router.routeInfoProvider(), | ||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||
| await tester.pumpAndSettle(); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| expect(find.text('Home'), findsOneWidget); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| unawaited( | ||||||||||||||||||||||||||||||||||
| router.push(const PageRouteInfo('SecondRoute', path: '/second')), | ||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||
| await tester.pumpAndSettle(); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| expect(find.text('Second'), findsOneWidget); | ||||||||||||||||||||||||||||||||||
| expect(router.stack, hasLength(2)); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| triggerRemoval.value = true; | ||||||||||||||||||||||||||||||||||
| router.notifyListeners(); | ||||||||||||||||||||||||||||||||||
| await tester.pump(); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| expect(tester.takeException(), isNull); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| await tester.pumpAndSettle(); | ||||||||||||||||||||||||||||||||||
| expect(tester.takeException(), isNull); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| expect(find.text('Home'), findsOneWidget); | ||||||||||||||||||||||||||||||||||
| expect(find.text('Second'), findsNothing); | ||||||||||||||||||||||||||||||||||
| expect(router.stack, hasLength(1)); | ||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| testWidgets( | ||||||||||||||||||||||||||||||||||
| 'a real system/browser back navigation does not throw ' | ||||||||||||||||||||||||||||||||||
| '"setState() called during build"', | ||||||||||||||||||||||||||||||||||
| (tester) async { | ||||||||||||||||||||||||||||||||||
| final triggerRemoval = ValueNotifier<bool>(false); | ||||||||||||||||||||||||||||||||||
| final router = _TestRouter(triggerRemoval); | ||||||||||||||||||||||||||||||||||
| addTearDown(triggerRemoval.dispose); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| await tester.pumpWidget( | ||||||||||||||||||||||||||||||||||
| MaterialApp.router( | ||||||||||||||||||||||||||||||||||
| routerDelegate: router.delegate( | ||||||||||||||||||||||||||||||||||
| initialRoutes: [ | ||||||||||||||||||||||||||||||||||
| const PageRouteInfo('HomeRoute', path: '/'), | ||||||||||||||||||||||||||||||||||
| ], | ||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||
| routeInformationParser: router.defaultRouteParser(), | ||||||||||||||||||||||||||||||||||
| routeInformationProvider: router.routeInfoProvider(), | ||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||
| await tester.pumpAndSettle(); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| unawaited( | ||||||||||||||||||||||||||||||||||
| router.push(const PageRouteInfo('SecondRoute', path: '/second')), | ||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||
| await tester.pumpAndSettle(); | ||||||||||||||||||||||||||||||||||
| expect(find.text('Second'), findsOneWidget); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| await tester.binding.handlePopRoute(); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| for (var i = 0; i < 10; i++) { | ||||||||||||||||||||||||||||||||||
| await tester.pump(const Duration(milliseconds: 50)); | ||||||||||||||||||||||||||||||||||
| expect(tester.takeException(), isNull); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [low] This second test passes on I ran the three new test files against the pre-fix It is also vacuous in a second way: the only assertion is
Suggested change
Worth flagging in the PR description too, since it is presented as the end-to-end check for #1184 — the only test that actually reproduces the reported exception is test #1, which triggers the removal from a descendant's |
||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Early return skips the redirect-guard cleanup and child-router cleanup when the page is already gone from
_pages.removeWhere(and_reset/updateDeclarativeRoutes) take pages out of_pagesdirectly. They never call_removeRedirectGuardor_removeTopRouterOf. Before this PR, when the Navigator then dropped that page,onDidRemovePage->removeRoutehitpageIndex == -1but still ran the guard loop and_removeTopRouterOf, so the listener got cleaned up there. With this early return that never happens.Scenario: a route guarded by a
RedirectGuardis removed withrouter.removeWhere(...). Its listener stays in_redirectGuardsListeners. The next time the guard callsnotifyListeners()(for example on an auth state change),_reevaluateruns on this router. No guarded route is left in the stack, so it goes straight to_strategy.reevaluate(...), and that can re-push routes orremoveAllAndPushfor a page the user already left.Suggestion: skip only the
_pages.removeAtand the notification whenpageIndex == -1. Keep the cleanup, but make it null-safe. For example,_removeRedirectGuardshould do nothing if_redirectGuardsListeners[guard]is null.