of<T extends Bloc> static method

T of<T extends Bloc>(
  1. BuildContext context
)

Implementation

static T of<T extends Bloc>(BuildContext context) {
  final BlocProvider<T>? provider =
      context.dependOnInheritedWidgetOfExactType<BlocProvider<T>>();

  if (provider == null) {
    if (kDebugMode) {
      print("BlocProvider of type [$T] not found in context");
      print(
          "Make sure your BlocProvider.of() call is inside the build method of a widget that is an ancestor of the BlocProvider");
      print("Or wrap your widget inside a builder.");
      print("For example:");
      print("""BlocProvider(
          create: () => CounterBloc(),
          child: Builder(
            builder: (context) {
              return Column();
            },
          ),
        ),""");
    }

    throw Exception("BlocProvider of type [$T] not found in context");
  }
  return provider.value;
}