Skip to content

Commit 8ad3fde

Browse files
[go_router] Add GoRouter.maybeOf (flutter#3216)
* feat: Add GoRouter.maybeOf * docs: Update the version number * test: Add tests on GoRouter.of and GoRouter.maybeOf
1 parent 13ee644 commit 8ad3fde

File tree

4 files changed

+74
-1
lines changed

4 files changed

+74
-1
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+
## 6.1.0
2+
3+
- Adds `GoRouter.maybeOf` to get the closest `GoRouter` from the context, if there is any.
4+
15
## 6.0.10
26

37
- Adds helpers for go_router_builder for ShellRoute support

packages/go_router/lib/src/router.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,13 @@ class GoRouter extends ChangeNotifier implements RouterConfig<RouteMatchList> {
301301
return inherited!.goRouter;
302302
}
303303

304+
/// The current GoRouter in the widget tree, if any.
305+
static GoRouter? maybeOf(BuildContext context) {
306+
final InheritedGoRouter? inherited =
307+
context.dependOnInheritedWidgetOfExactType<InheritedGoRouter>();
308+
return inherited?.goRouter;
309+
}
310+
304311
@override
305312
void dispose() {
306313
_routeInformationProvider.dispose();

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: 6.0.10
4+
version: 6.1.0
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

packages/go_router/test/go_router_test.dart

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3275,6 +3275,68 @@ void main() {
32753275
});
32763276
});
32773277
});
3278+
3279+
group('of', () {
3280+
testWidgets(
3281+
'It should return the go router instance of the widget tree',
3282+
(WidgetTester tester) async {
3283+
const Key key = Key('key');
3284+
final List<RouteBase> routes = <RouteBase>[
3285+
GoRoute(
3286+
path: '/',
3287+
builder: (_, __) => const SizedBox(key: key),
3288+
),
3289+
];
3290+
3291+
final GoRouter router = await createRouter(routes, tester);
3292+
final Element context = tester.element(find.byKey(key));
3293+
final GoRouter foundRouter = GoRouter.of(context);
3294+
expect(foundRouter, router);
3295+
},
3296+
);
3297+
3298+
testWidgets(
3299+
'It should throw if there is no go router in the widget tree',
3300+
(WidgetTester tester) async {
3301+
const Key key = Key('key');
3302+
await tester.pumpWidget(const SizedBox(key: key));
3303+
3304+
final Element context = tester.element(find.byKey(key));
3305+
expect(() => GoRouter.of(context), throwsA(anything));
3306+
},
3307+
);
3308+
});
3309+
3310+
group('maybeOf', () {
3311+
testWidgets(
3312+
'It should return the go router instance of the widget tree',
3313+
(WidgetTester tester) async {
3314+
const Key key = Key('key');
3315+
final List<RouteBase> routes = <RouteBase>[
3316+
GoRoute(
3317+
path: '/',
3318+
builder: (_, __) => const SizedBox(key: key),
3319+
),
3320+
];
3321+
3322+
final GoRouter router = await createRouter(routes, tester);
3323+
final Element context = tester.element(find.byKey(key));
3324+
final GoRouter? foundRouter = GoRouter.maybeOf(context);
3325+
expect(foundRouter, router);
3326+
},
3327+
);
3328+
3329+
testWidgets(
3330+
'It should return null if there is no go router in the widget tree',
3331+
(WidgetTester tester) async {
3332+
const Key key = Key('key');
3333+
await tester.pumpWidget(const SizedBox(key: key));
3334+
3335+
final Element context = tester.element(find.byKey(key));
3336+
expect(GoRouter.maybeOf(context), isNull);
3337+
},
3338+
);
3339+
});
32783340
}
32793341

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

0 commit comments

Comments
 (0)