of method

T of(
  1. BuildContext context
)

An extension method of Pot that recursively visits ancestors to find the nearest LocalPottery and obtains the object bound locally to the pot.

If the pot which this method is called on is contained as a key in the pots map of a LocalPottery located up in the tree, the value corresponding to the key is obtained from there.

If no such LocalPottery is found, the object held in the global pot is returned, in which case, the return value is the same as that of the Pot.call method.

See the document of LocalPottery for usage.

Note that calling this method is relatively expensive (O(N) in the depth of the tree). Only call it if the distance from the widget associated with the BuildContext to the desired ancestor is known to be small and bounded.

Implementation

T of(BuildContext context) {
  // Targets the current BuildContext too so that the local objects
  // become available from within the builder callback.
  var (:object, :found) = _findObject(context);

  if (!found) {
    context.visitAncestorElements((element) {
      // Suppresses false positive warning
      // ignore: unnecessary_statements
      (:object, :found) = _findObject(element);
      return !found;
    });
  }

  return found ? object as T : this();
}