Skip to content

Commit

Permalink
[go_router] fix context extension for replaceNamed (#3927)
Browse files Browse the repository at this point in the history
The extension method is not passing all the data to the function, such as pathParams and queryParams, fixes: flutter/flutter#126222
  • Loading branch information
Dreamersoul authored May 19, 2023
1 parent e84b49c commit 1dea5f9
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
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'));
}
}

0 comments on commit 1dea5f9

Please sign in to comment.