containerOf static method

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;
}