maybeOf<T extends Object?> method Null safety

Router<T>? maybeOf<T extends Object?>(
  1. BuildContext context
)

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.

Implementation

static Router<T>? maybeOf<T extends Object?>(BuildContext context) {
  final _RouterScope? scope = context.dependOnInheritedWidgetOfExactType<_RouterScope>();
  return scope?.routerState.widget as Router<T>?;
}