Skip to content

Commit 1dea5f9

Browse files
authored
[go_router] fix context extension for replaceNamed (#3927)
The extension method is not passing all the data to the function, such as pathParams and queryParams, fixes: flutter/flutter#126222
1 parent e84b49c commit 1dea5f9

File tree

4 files changed

+65
-2
lines changed

4 files changed

+65
-2
lines changed

packages/go_router/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 7.0.2
2+
3+
- Fixes `BuildContext` extension method `replaceNamed` to correctly pass `pathParameters` and `queryParameters`.
4+
15
## 7.0.1
26

37
- Adds a workaround for the `dart fix --apply` issue, https://github.com/dart-lang/sdk/issues/52233.

packages/go_router/lib/src/misc/extensions.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,5 +130,8 @@ extension GoRouterHelper on BuildContext {
130130
Map<String, dynamic> queryParameters = const <String, dynamic>{},
131131
Object? extra,
132132
}) =>
133-
GoRouter.of(this).replaceNamed(name, extra: extra);
133+
GoRouter.of(this).replaceNamed(name,
134+
pathParameters: pathParameters,
135+
queryParameters: queryParameters,
136+
extra: extra);
134137
}

packages/go_router/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: go_router
22
description: A declarative router for Flutter based on Navigation 2 supporting
33
deep linking, data-driven routes and more
4-
version: 7.0.1
4+
version: 7.0.2
55
repository: https://github.com/flutter/packages/tree/main/packages/go_router
66
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+go_router%22
77

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter/material.dart';
6+
import 'package:flutter_test/flutter_test.dart';
7+
import 'package:go_router/go_router.dart';
8+
9+
void main() {
10+
group('replaceNamed', () {
11+
Future<GoRouter> createGoRouter(
12+
WidgetTester tester, {
13+
Listenable? refreshListenable,
14+
}) async {
15+
final GoRouter router = GoRouter(
16+
initialLocation: '/',
17+
routes: <GoRoute>[
18+
GoRoute(
19+
path: '/',
20+
name: 'home',
21+
builder: (_, __) => const _MyWidget(),
22+
),
23+
GoRoute(
24+
path: '/page-0/:tab',
25+
name: 'page-0',
26+
builder: (_, __) => const SizedBox())
27+
],
28+
);
29+
await tester.pumpWidget(MaterialApp.router(
30+
routerConfig: router,
31+
));
32+
return router;
33+
}
34+
35+
testWidgets('Passes GoRouter parameters through context call.',
36+
(WidgetTester tester) async {
37+
final GoRouter router = await createGoRouter(tester);
38+
await tester.tap(find.text('Settings'));
39+
await tester.pumpAndSettle();
40+
expect(router.location, '/page-0/settings?search=notification');
41+
});
42+
});
43+
}
44+
45+
class _MyWidget extends StatelessWidget {
46+
const _MyWidget();
47+
48+
@override
49+
Widget build(BuildContext context) {
50+
return ElevatedButton(
51+
onPressed: () => context.replaceNamed('page-0',
52+
pathParameters: <String, String>{'tab': 'settings'},
53+
queryParameters: <String, String>{'search': 'notification'}),
54+
child: const Text('Settings'));
55+
}
56+
}

0 commit comments

Comments
 (0)