of static method

The state from the closest instance of this class that encloses the given context.

Typical usage is as follows:

Router.of(context).push('/my/path');

If there is no Router in the give context, this function will throw a AlbaError in debug mode, and an exception in release mode.

This method can be expensive (it walks the element tree).

Implementation

static RouterWidgetState of(BuildContext context) {
  var routerState = context.findRootAncestorStateOfType<RouterWidgetState>();

  assert(() {
    if (routerState == null) {
      throw AlbaError(
          'Router operation requested with a context that does not include a Router.\n');
    }
    return true;
  }());

  return routerState!;
}