MultiBlocListener constructor

MultiBlocListener({
  1. Key? key,
  2. required List<BlocListenerSingleChildWidget> listeners,
  3. required Widget child,
})

Merges multiple BlocListener widgets into one widget tree.

MultiBlocListener improves the readability and eliminates the need to nest multiple BlocListeners.

By using MultiBlocListener we can go from:

BlocListener<BlocA, BlocAState>(
  listener: (context, state) {},
  child: BlocListener<BlocB, BlocBState>(
    listener: (context, state) {},
    child: BlocListener<BlocC, BlocCState>(
      listener: (context, state) {},
      child: ChildA(),
    ),
  ),
)

to:

MultiBlocListener(
  listeners: [
    BlocListener<BlocA, BlocAState>(
      listener: (context, state) {},
    ),
    BlocListener<BlocB, BlocBState>(
      listener: (context, state) {},
    ),
    BlocListener<BlocC, BlocCState>(
      listener: (context, state) {},
    ),
  ],
  child: ChildA(),
)

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

Implementation

MultiBlocListener({
  Key? key,
  required List<BlocListenerSingleChildWidget> listeners,
  required Widget child,
}) : super(key: key, providers: listeners, child: child);