containerOf method Null safety

ProviderContainer containerOf(
  1. BuildContext context,
  2. {bool listen = true}
)

Read the current ProviderContainer for a BuildContext.

Implementation

static ProviderContainer containerOf(
  BuildContext context, {
  bool listen = true,
}) {
  UncontrolledProviderScope? scope;

  if (listen) {
    scope = context //
        .dependOnInheritedWidgetOfExactType<UncontrolledProviderScope>();
  } else {
    scope = context
        .getElementForInheritedWidgetOfExactType<UncontrolledProviderScope>()
        ?.widget as UncontrolledProviderScope?;
  }

  if (scope == null) {
    throw StateError('No ProviderScope found');
  }

  return scope.container;
}