BlocProvider<T extends BlocBase<Object?>> constructor

BlocProvider<T extends BlocBase<Object?>>({
  1. Key? key,
  2. required Create<T> create,
  3. Widget? child,
  4. bool? lazy,
})

Takes a Create function that is responsible for creating the Bloc or Cubit and a child which will have access to the instance via BlocProvider.of(context). It is used as a dependency injection (DI) widget so that a single instance of a Bloc or Cubit can be provided to multiple widgets within a subtree.

BlocProvider(
  create: (BuildContext context) => BlocA(),
  child: ChildA(),
);

It automatically handles closing the instance when used with Create. By default, Create is called only when the instance is accessed. To override this behavior, set lazy to false.

BlocProvider(
  lazy: false,
  create: (BuildContext context) => BlocA(),
  child: ChildA(),
);

Implementation

BlocProvider({
  Key? key,
  required Create<T> create,
  this.child,
  this.lazy,
})  : _create = create,
      _value = null,
      super(key: key, child: child);