maybeOf<G extends GetAny> static method

G? maybeOf<G extends GetAny>(
  1. BuildContext context,
  2. G get, {
  3. bool createDependency = true,
})

If the Get object is overridden in an ancestor Ref, returns that object.

Returns null otherwise.

Implementation

static G? maybeOf<G extends GetAny>(
  BuildContext context,
  G get, {
  bool createDependency = true,
}) {
  final ScopeModel? model =
      createDependency
          ? context.dependOnInheritedWidgetOfExactType()
          : context.getInheritedWidgetOfExactType();

  assert(() {
    final ValueRef? scoped = model?.map[get];
    if (scoped is G?) return true;

    throw FlutterError.fromParts([
      ErrorSummary('An invalid substitution was made for a $G.'),
      ErrorDescription('A ${get.runtimeType} was substituted with a ${scoped.runtimeType}.'),
      if (Ref(get).debugSubWidget(context) case final widget?) ...[
        ErrorDescription('The invalid substitution was made by the following widget:'),
        widget.toDiagnosticsNode(style: DiagnosticsTreeStyle.error),
      ],
    ]);
  }());

  return model?._select(get);
}