Unverified Commit 85a25256 authored by Greg Spencer's avatar Greg Spencer Committed by GitHub

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

This removes the nullOk parameter from Router.of, and createsRouter.maybeOf. Router.ofnow returns a non-nullable value, and the Router.maybeOf returns a nullable value.
parent 4f586fa3
......@@ -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 '
......@@ -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;
}
......
......@@ -77,7 +77,7 @@ 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)
......@@ -85,7 +85,7 @@ void main() {
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.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment