of<T> static method

T of<T>(
  1. BuildContext context, {
  2. bool rebuild = false,
})

Retrieves the dependency of type T from the nearest ancestor Binder widget.

Implementation

static T of<T>(
  BuildContext context, {
  bool rebuild = false,
  // Object Function(T value)? filter,
}) {
  final BindElement<T>? inheritedElement =
      context.getElementForInheritedWidgetOfExactType<Binder<T>>()
          as BindElement<T>?;

  if (inheritedElement == null) {
    throw BindError<String>(controller: "$T");
  }

  if (rebuild) {
    context.dependOnInheritedElement(inheritedElement);
  }

  final T controller = inheritedElement.controller;

  return controller;
}