BlocProvider<T extends StateStreamableSource<Object?>> constructor

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

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

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