Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/go_router/lib/src/delegate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ class GoRouterDelegate extends RouterDelegate<RouteMatchList> with ChangeNotifie

final RouteConfiguration _configuration;

/// Called with the new [currentConfiguration] after a page is popped.
///
/// [GoRouter] uses it to keep its route information provider in sync.
@internal
ValueChanged<RouteMatchList>? onRouteMatchListPopped;

@override
Future<bool> popRoute() async {
final Iterable<NavigatorState> states = _findCurrentNavigators();
Expand Down Expand Up @@ -192,6 +198,7 @@ class GoRouterDelegate extends RouterDelegate<RouteMatchList> with ChangeNotifie

// Unconditionally remove the match from the current configuration
currentConfiguration = currentConfiguration.remove(match);
onRouteMatchListPopped?.call(currentConfiguration);

notifyListeners();

Expand Down
20 changes: 20 additions & 0 deletions packages/go_router/lib/src/information_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,26 @@ class GoRouteInformationProvider extends RouteInformationProvider
);
}

/// Sets [value] to `matchList`, as [restore] does, without notifying
/// listeners.
///
/// Called when a page is popped without [GoRouter.pop], for example by
/// `Navigator.pop` or a system back gesture. The [Router] only reports the
/// new location after the frame, so until then [value] still describes the
/// popped page, and a refresh in that window would bring it back. The
/// router is already showing `matchList`, so there is nothing to re-parse.
@internal
void syncWithPoppedMatchList(RouteMatchList matchList) {
_value = RouteInformation(
uri: matchList.uri,
state: RouteInformationState<void>(
extra: matchList.extra,
baseRouteMatchList: matchList,
type: NavigatingType.restore,
),
);
}

/// Removes the top-most route match from `base` and pushes the `location` as a
/// new route on top.
Future<T?> pushReplacement<T>(String location, {required RouteMatchList base, Object? extra}) {
Expand Down
2 changes: 1 addition & 1 deletion packages/go_router/lib/src/router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ class GoRouter implements RouterConfig<RouteMatchList> {
// allowing the caller to wrap the navigator themselves
builderWithNav: (BuildContext context, Widget child) =>
InheritedGoRouter(goRouter: this, child: child),
);
)..onRouteMatchListPopped = routeInformationProvider.syncWithPoppedMatchList;

assert(() {
log('setting initial location $initialLocation');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
changelog: |
- Fixes a page popped with `Navigator.pop`, a back button, or a system back gesture being shown again when `refreshListenable` notifies before the next frame.
version: patch

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.

This change introduces a new feature, so it should be a minor version release.

32 changes: 32 additions & 0 deletions packages/go_router/test/delegate_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,38 @@ void main() {
addTearDown(goRouter.dispose);
});

testWidgets('Navigator.pop keeps the route information in sync', (WidgetTester tester) async {
final valueNotifier = ValueNotifier<int>(0);
addTearDown(valueNotifier.dispose);
final GoRouter goRouter = await createGoRouter(tester, refreshListenable: valueNotifier);

goRouter.push('/a');
await tester.pumpAndSettle();

// Pops the page without GoRouter.pop, as an AppBar back button does.
goRouter.routerDelegate.navigatorKey.currentState!.pop();
// A refresh before the next frame re-parses the route information.
valueNotifier.notifyListeners();
await tester.pumpAndSettle();

expect(goRouter.routerDelegate.currentConfiguration.matches.last.matchedLocation, '/');
});

testWidgets('system back keeps the route information in sync', (WidgetTester tester) async {
final valueNotifier = ValueNotifier<int>(0);
addTearDown(valueNotifier.dispose);
final GoRouter goRouter = await createGoRouter(tester, refreshListenable: valueNotifier);

goRouter.push('/a');
await tester.pumpAndSettle();

await tester.binding.handlePopRoute();
valueNotifier.notifyListeners();
await tester.pumpAndSettle();

expect(goRouter.routerDelegate.currentConfiguration.matches.last.matchedLocation, '/');
});

testWidgets('removes the last element', (WidgetTester tester) async {
final GoRouter goRouter = await createGoRouter(tester)
..push('/error');
Expand Down
20 changes: 20 additions & 0 deletions packages/go_router/test/go_router_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1225,6 +1225,26 @@ void main() {
]);
});

testWidgets('on Navigator.pop', (WidgetTester tester) async {
final routes = <GoRoute>[
GoRoute(
path: '/',
builder: (_, _) => const DummyScreen(),
routes: <RouteBase>[GoRoute(path: 'settings', builder: (_, _) => const DummyScreen())],
),
];

final GoRouter router = await createRouter(routes, tester, initialLocation: '/settings');
final codec = RouteMatchListCodec(router.configuration);
log.clear();
router.routerDelegate.navigatorKey.currentState!.pop();
await tester.pumpAndSettle();
expect(log, <Object>[
isMethodCall('selectMultiEntryHistory', arguments: null),
IsRouteUpdateCall('/', false, codec.encode(router.routerDelegate.currentConfiguration)),
]);
});

testWidgets('on pop twice', (WidgetTester tester) async {
final routes = <GoRoute>[
GoRoute(
Expand Down
Loading