Skip to content

[go_router] Add GoRouter.maybeOf #3216

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 5 commits into from
Feb 23, 2023
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 @@
## 6.1.0

- Adds `GoRouter.maybeOf` to get the closest `GoRouter` from the context, if there is any.

## 6.0.10

- Adds helpers for go_router_builder for ShellRoute support
Expand Down
7 changes: 7 additions & 0 deletions packages/go_router/lib/src/router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,13 @@ class GoRouter extends ChangeNotifier implements RouterConfig<RouteMatchList> {
return inherited!.goRouter;
}

/// The current GoRouter in the widget tree, if any.
static GoRouter? maybeOf(BuildContext context) {
final InheritedGoRouter? inherited =
context.dependOnInheritedWidgetOfExactType<InheritedGoRouter>();
return inherited?.goRouter;
}

@override
void dispose() {
_routeInformationProvider.dispose();
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: 6.0.10
version: 6.1.0
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
62 changes: 62 additions & 0 deletions packages/go_router/test/go_router_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3275,6 +3275,68 @@ void main() {
});
});
});

group('of', () {
testWidgets(
'It should return the go router instance of the widget tree',
(WidgetTester tester) async {
const Key key = Key('key');
final List<RouteBase> routes = <RouteBase>[
GoRoute(
path: '/',
builder: (_, __) => const SizedBox(key: key),
),
];

final GoRouter router = await createRouter(routes, tester);
final Element context = tester.element(find.byKey(key));
final GoRouter foundRouter = GoRouter.of(context);
expect(foundRouter, router);
},
);

testWidgets(
'It should throw if there is no go router in the widget tree',
(WidgetTester tester) async {
const Key key = Key('key');
await tester.pumpWidget(const SizedBox(key: key));

final Element context = tester.element(find.byKey(key));
expect(() => GoRouter.of(context), throwsA(anything));
},
);
});

group('maybeOf', () {
testWidgets(
'It should return the go router instance of the widget tree',
(WidgetTester tester) async {
const Key key = Key('key');
final List<RouteBase> routes = <RouteBase>[
GoRoute(
path: '/',
builder: (_, __) => const SizedBox(key: key),
),
];

final GoRouter router = await createRouter(routes, tester);
final Element context = tester.element(find.byKey(key));
final GoRouter? foundRouter = GoRouter.maybeOf(context);
expect(foundRouter, router);
},
);

testWidgets(
'It should return null if there is no go router in the widget tree',
(WidgetTester tester) async {
const Key key = Key('key');
await tester.pumpWidget(const SizedBox(key: key));

final Element context = tester.element(find.byKey(key));
expect(GoRouter.maybeOf(context), isNull);
},
);
});
}

/// This allows a value of type T or T? to be treated as a value of type T?.
Expand Down