Bloc: Extremely strange bug

Created on 30 Dec 2019  ·  7Comments  ·  Source: felangel/bloc

Describe the bug
Well, I have this code below:

@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"),
              );
            },
          ),
        );
      },
    ),
  );
}

I thought this code is easy and straightforward, I expected those 2 blocs to be instantiated. However, that's the output I see in the logcat:

homeBloc: Instance of 'HomeBloc'
profileBloc: null

It seems like MultiBlocProvider doesn't instantiate all BLOCs.
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.

PS I'm sorry for asking it here but I got no response on stack overflow.

question

Most helpful comment

@bartekpacia I've opened a pull request to fix the issue. The problem is your ProfilePageBloc throws an AssertionError because you aren't providing a user when you create it in BlocProvider. As a result, the ProfilePageBloc is never created and cannot be retrieved. Hope that helps!

All 7 comments

Hi @bartekpacia 👋
Thanks for opening an issue!

Are you able to provide a link to a sample app which illustrates the issue? Thanks and sorry for the inconvenience!

Sure, I'll try to create minimal nonworking example

Okay, done.
I tried to cut as much unnecessary code as possible.
demo
Strange thing is that for the very first time a different error is shown:
1
Then, every next error is this:
2

The code I posted above is in lib/home/home_screen.dart.

If you quickly clone and try to run this dummy app and maybe give me some tips, I'll be extremely happy and extremely grateful!

Will take a look shortly! Thanks for providing the sample 🙏

@bartekpacia I've opened a pull request to fix the issue. The problem is your ProfilePageBloc throws an AssertionError because you aren't providing a user when you create it in BlocProvider. As a result, the ProfilePageBloc is never created and cannot be retrieved. Hope that helps!

Wow, such a small thing but wasted me about 6 hours.
Thank you very much, really, I can't it express with words. If not you I'd probably still be sitting and trying to figure out wtf is happening. Even now, after using your fix, it turned out that I have some another problem with silly circular-state, so to say. App was getting into an infinite loop ProfilePageLoading -> ProfilePageShowed -> ProfilePageSuccess -> ProfilePageLoading, because I was emmitting an event which triggered ProfilePageLoading in build method of ProfilePage. Fortunately I fixed it.

I'd never guess that failed assertion could cause this. So just a little follow-up question – why didn't I see a gigantic Assertion failed red screen like usually? Not even in logs.

Thanks again!

Unfortunately I think provider 4.0.0 changed the error handling and now just swallows the error. You can open an issue on provider for this if you'd like. Glad I was able to help!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

clicksocial picture clicksocial  ·  3Comments

RobPFarley picture RobPFarley  ·  3Comments

nerder picture nerder  ·  3Comments

shawnchan2014 picture shawnchan2014  ·  3Comments

wheel1992 picture wheel1992  ·  3Comments