of<G extends GetAny> static method

G of<G extends GetAny>(
  1. BuildContext context,
  2. G get, {
  3. bool createDependency = true,
  4. bool throwIfMissing = false,
})

Returns the Get object that overrides this one.

If no such object is found, returns the object provided as input, or throws an error if throwIfMissing is true.

See also:

  • GetScope.maybeOf, which returns null if the relevant Override is not found in the ancestor Ref.

Implementation

static G of<G extends GetAny>(
  BuildContext context,
  G get, {
  bool createDependency = true,
  bool throwIfMissing = false,
}) {
  assert(() {
    if (!throwIfMissing || maybeOf(context, get) != null) return true;

    final GetScope? ancestor = context.getInheritedWidgetOfExactType();
    throw FlutterError.fromParts([
      if (ancestor == null)
        ErrorSummary('No ancestor Ref found.')
      else
        ErrorSummary('The $G was not found in the ancestor Ref.'),
      ErrorHint(
        'Double-check that the provided context contains an ancestor Ref '
        'with the appropriate Override.',
      ),
    ]);
  }());
  return maybeOf(context, get, createDependency: createDependency) ?? get;
}