of static method

ObservableLocator of(
  1. BuildContext context, {
  2. bool listen = true,
})

The ObservableLocator of the closest ObservableLocatorScope that surrounds the given context.

If listen is true, any changes to the surrounding observable locator scope will trigger a new State.build to widgets, and State.didChangeDependencies for StatefulWidget.

listen: false is necessary to be able to call this method inside of State.initState.

Implementation

static ObservableLocator of(BuildContext context, {bool listen = true}) {
  assert(_debugCheckHasLocator(context));

  final element = context.getElementForInheritedWidgetOfExactType<
      _InheritedObservableLocatorScope>()!;

  if (listen) {
    context.dependOnInheritedElement(element);
  }

  return (element.widget as _InheritedObservableLocatorScope).locator;
}