MultiBlocProvider constructor

MultiBlocProvider({
  1. required List<SingleChildWidget> providers,
  2. required Widget child,
  3. Key? key,
})

Merges multiple BlocProvider widgets into one widget tree.

MultiBlocProvider improves the readability and eliminates the need to nest multiple BlocProviders.

By using MultiBlocProvider we can go from:

BlocProvider<BlocA>(
  create: (BuildContext context) => BlocA(),
  child: BlocProvider<BlocB>(
    create: (BuildContext context) => BlocB(),
    child: BlocProvider<BlocC>(
      create: (BuildContext context) => BlocC(),
      child: ChildA(),
    )
  )
)

to:

MultiBlocProvider(
  providers: [
    BlocProvider<BlocA>(
      create: (BuildContext context) => BlocA(),
    ),
    BlocProvider<BlocB>(
      create: (BuildContext context) => BlocB(),
    ),
    BlocProvider<BlocC>(
      create: (BuildContext context) => BlocC(),
    ),
  ],
  child: ChildA(),
)

MultiBlocProvider converts the BlocProvider list into a tree of nested BlocProvider widgets. As a result, the only advantage of using MultiBlocProvider is improved readability due to the reduction in nesting and boilerplate.

Implementation

MultiBlocProvider({
  required List<SingleChildWidget> providers,
  required Widget child,
  Key? key,
}) : super(key: key, providers: providers, child: child);