Skip to content
Merged
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
4 changes: 4 additions & 0 deletions packages/go_router/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 4.2.3

- Fixes a bug where the ValueKey to be the same when a page was pushed multiple times.

## 4.2.2

- Fixes a bug where go_router_builder wasn't detecting annotations.
Expand Down
21 changes: 19 additions & 2 deletions packages/go_router/lib/src/delegate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,27 @@ class GoRouterDelegate extends RouterDelegate<RouteMatchList>
final GlobalKey<NavigatorState> _key = GlobalKey<NavigatorState>();

RouteMatchList _matches = RouteMatchList.empty();
final Map<String, int> _pushCounts = <String, int>{};

/// Push the given location onto the page stack
/// Pushes the given location onto the page stack
void push(RouteMatch match) {
_matches.push(match);
// Remap the pageKey to allow any number of the same page on the stack
final String fullPath = match.fullpath;
final int count = (_pushCounts[fullPath] ?? 0) + 1;
_pushCounts[fullPath] = count;
final ValueKey<String> pageKey = ValueKey<String>('$fullPath-p$count');
final RouteMatch newPageKeyMatch = RouteMatch(
route: match.route,
subloc: match.subloc,
fullpath: match.fullpath,
encodedParams: match.encodedParams,
queryParams: match.queryParams,
extra: match.extra,
error: match.error,
pageKey: pageKey,
);

_matches.push(newPageKeyMatch);
notifyListeners();
}

Expand Down
2 changes: 1 addition & 1 deletion packages/go_router/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: go_router
description: A declarative router for Flutter based on Navigation 2 supporting
deep linking, data-driven routes and more
version: 4.2.2
version: 4.2.3
repository: https://github.com/flutter/packages/tree/main/packages/go_router
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+go_router%22

Expand Down
33 changes: 33 additions & 0 deletions packages/go_router/test/delegate_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Future<GoRouter> createGoRouter(
initialLocation: '/',
routes: <GoRoute>[
GoRoute(path: '/', builder: (_, __) => const DummyStatefulWidget()),
GoRoute(path: '/a', builder: (_, __) => const DummyStatefulWidget()),
GoRoute(
path: '/error',
builder: (_, __) => const ErrorScreen(null),
Expand Down Expand Up @@ -56,6 +57,38 @@ void main() {
});
});

group('push', () {
testWidgets(
'It should return different pageKey when push is called',
(WidgetTester tester) async {
final GoRouter goRouter = await createGoRouter(tester);
expect(goRouter.routerDelegate.matches.matches.length, 1);
expect(
goRouter.routerDelegate.matches.matches[0].pageKey,
null,
);

goRouter.push('/a');
Copy link
Contributor

Choose a reason for hiding this comment

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

can you do an await tester.pumpAndSettle() in between every push to verify it does not crash?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the review, I fixed it in 2ee5fd6.

await tester.pumpAndSettle();

expect(goRouter.routerDelegate.matches.matches.length, 2);
expect(
goRouter.routerDelegate.matches.matches[1].pageKey,
const Key('/a-p1'),
);

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

expect(goRouter.routerDelegate.matches.matches.length, 3);
expect(
goRouter.routerDelegate.matches.matches[2].pageKey,
const Key('/a-p2'),
);
},
);
});

group('canPop', () {
testWidgets(
'It should return false if there is only 1 match in the stack',
Expand Down