0

So I have this code

@override
Widget build(BuildContext context) {
  return MultiBlocProvider(
    providers: [
      BlocProvider<ProfilePageBloc>(
        create: (context) => ProfilePageBloc(userRepository: UserRepository.instance),
      ),
      BlocProvider<HomeBloc>(
        create: (context) => HomeBloc(),
      ),
    ],
    child: BlocBuilder<HomeBloc, HomeState>(
      builder: (context, state) {
        return Scaffold(
          body: BlocBuilder<HomeBloc, HomeState>(
            builder: (context, state) {

              if (state is StateThatDoesNotMatter) {
                final HomeBloc homeBloc = BlocProvider.of<HomeBloc>(context);
                final ProfilePageBloc profileBloc = BlocProvider.of<ProfilePageBloc>(context);
                print("homeBloc: ${homeBloc.toString()}");
                print("profileBloc: ${profileBloc.toString()}");


              return Center(
                child: Text("Doesn't really matter"),
              );
            },
          ),
        );
      },
    ),
  );
}

and the output I get is

homeBloc: Instance of 'HomeBloc'
profileBloc: null

I expected profileBloc to be instantiated like homeBloc was.

This makes me completely unable to continue development. I have no idea why it's like that. The best thing is, it worked a few times, but I wasn't able to reproduce this behavior.

Any help would be greatly appreciated

Bartek Pacia
  • 1,085
  • 3
  • 15
  • 37

1 Answers1

0

It turned out that 'ProfilePageBloc` wasn't instantiated because an assertion was failing in its constructor. Code I've provided in the quesiton wasn't enough to solve it.

More info here

Bartek Pacia
  • 1,085
  • 3
  • 15
  • 37