maybeOf<T extends Service> static method

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

Returns the Service of type T from the closest ancestor ServiceScope.

Returns null if no provider is found. To throw an error instead, you can implement a separate of method with assert/exception.

Implementation

static T? maybeOf<T extends Service>(BuildContext context) {
  final scope = ServiceScope.maybeOf(context);
  final value = scope?.services[T];

  if (value is T) return value;
  return null;
}