Skip to content
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

Remove nullOk parameter from Router.of and make it return a non-nullable value #68910

Merged
merged 1 commit into from Oct 29, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 28 additions & 2 deletions packages/flutter/lib/src/widgets/router.dart
Expand Up @@ -298,10 +298,18 @@ class Router<T> extends StatefulWidget {
/// This method provides access to the delegates in the [Router]. For example,
/// this can be used to access the [backButtonDispatcher] of the parent router
/// when creating a [ChildBackButtonDispatcher] for a nested [Router].
static Router<dynamic>? of(BuildContext context, {bool nullOk = false}) {
///
/// If no [Router] ancestor exists for the given context, this will assert in
/// debug mode, and throw an exception in release mode.
///
/// See also:
///
/// * [maybeOf], which is a similar function, but it will return null instead
/// of throwing an exception if no [Router] ancestor exists.
static Router<dynamic> of(BuildContext context) {
final _RouterScope? scope = context.dependOnInheritedWidgetOfExactType<_RouterScope>();
assert(() {
if (scope == null && !nullOk) {
if (scope == null) {
throw FlutterError(
'Router operation requested with a context that does not include a Router.\n'
'The context used to retrieve the Router must be that of a widget that '
Expand All @@ -310,6 +318,24 @@ class Router<T> extends StatefulWidget {
}
return true;
}());
return scope!.routerState.widget;
}

/// Retrieves the immediate [Router] ancestor from the given context.
///
/// This method provides access to the delegates in the [Router]. For example,
/// this can be used to access the [backButtonDispatcher] of the parent router
/// when creating a [ChildBackButtonDispatcher] for a nested [Router].
///
/// If no `Router` ancestor exists for the given context, this will return
/// null.
///
/// See also:
///
/// * [of], a similar method that returns a non-nullable value, and will
/// throw if no [Router] ancestor exists.
static Router<dynamic>? maybeOf(BuildContext context) {
final _RouterScope? scope = context.dependOnInheritedWidgetOfExactType<_RouterScope>();
return scope?.routerState.widget;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/flutter/test/widgets/router_test.dart
Expand Up @@ -77,15 +77,15 @@ void main() {
});
});

testWidgets('Router.of can be null', (WidgetTester tester) async {
testWidgets('Router.maybeOf can be null', (WidgetTester tester) async {
final GlobalKey key = GlobalKey();
await tester.pumpWidget(buildBoilerPlate(
Text('dummy', key: key)
));
final BuildContext textContext = key.currentContext!;

// This should not throw error.
Router<dynamic>? router = Router.of(textContext, nullOk: true);
Router<dynamic>? router = Router.maybeOf(textContext);
expect(router, isNull);

// Test when the nullOk is not specified.
Expand Down