Skip to content

[go_router] fix context extension for replaceNamed #3927

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

Merged
merged 13 commits into from
May 19, 2023
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 @@
## 7.0.2

- Fixes `BuildContext` extension method `replaceNamed` to correctly pass `pathParameters` and `queryParameters`.

## 7.0.1

- Adds a workaround for the `dart fix --apply` issue, https://github.com/dart-lang/sdk/issues/52233.
Expand Down
5 changes: 4 additions & 1 deletion packages/go_router/lib/src/misc/extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -130,5 +130,8 @@ extension GoRouterHelper on BuildContext {
Map<String, dynamic> queryParameters = const <String, dynamic>{},
Object? extra,
}) =>
GoRouter.of(this).replaceNamed(name, extra: extra);
GoRouter.of(this).replaceNamed(name,
pathParameters: pathParameters,
queryParameters: queryParameters,
extra: extra);
}
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: 7.0.1
version: 7.0.2
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
56 changes: 56 additions & 0 deletions packages/go_router/test/extension_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:go_router/go_router.dart';

void main() {
group('replaceNamed', () {
Future<GoRouter> createGoRouter(
WidgetTester tester, {
Listenable? refreshListenable,
}) async {
final GoRouter router = GoRouter(
initialLocation: '/',
routes: <GoRoute>[
GoRoute(
path: '/',
name: 'home',
builder: (_, __) => const _MyWidget(),
),
GoRoute(
path: '/page-0/:tab',
name: 'page-0',
builder: (_, __) => const SizedBox())
],
);
await tester.pumpWidget(MaterialApp.router(
routerConfig: router,
));
return router;
}

testWidgets('Passes GoRouter parameters through context call.',
(WidgetTester tester) async {
final GoRouter router = await createGoRouter(tester);
await tester.tap(find.text('Settings'));
await tester.pumpAndSettle();
expect(router.location, '/page-0/settings?search=notification');
});
});
}

class _MyWidget extends StatelessWidget {
const _MyWidget();

@override
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: () => context.replaceNamed('page-0',
pathParameters: <String, String>{'tab': 'settings'},
queryParameters: <String, String>{'search': 'notification'}),
child: const Text('Settings'));
}
}