Two successive BLOCs in flutter_bloc

Issue

I want to split my BLOC into two BLOCs:

  1. the one who does preparing job
  2. the main one

Let’s call it PrepBloc and MainBloc. Is it reasonable way to put one inside another like this:

BlocProvider<PrepBloc>(
          create: ......,
          child: AppView()
        )
class AppView extends StatelessWidget {
  Widget build(BuildContext context) {
    return BlocBuilder<PrepBloc, PrepState>(
      builder: (context, state) {
        if (state is ReadyState) {
          return BlocProvider<MainBloc>(...);
        }
        else if (state is NotReadyState) {
          return PrepPage();
        }
      }
    );
  }
}

I know there is MultiBloc thing but it seems to me a little sophisticated and producing a lots of code.

Solution

That is absolutley a correct way to do it!

You would use a MultiBlocProvider if the children need access to both blocs, but in your case that isn’t what you are after. I guess there is no point for PrepPage to have access to MainBloc.

Answered By – Robert Sandberg

Answer Checked By – David Marino (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *