felangel/bloc.js

State not changing

kieronwiehahn opened this issue ยท 2 comments

Hi

Please can you assist me with an issue I'm still facing. I am still new to your Flutter_bloc package. I have adapted your Todo app tut to a simple home page that uses a floating action button to dynamically add a custom light button to Home. I have removed all the other features such as the bottom tab and filters as they are not needed. The problem I'm facing is the state never actually updates from the BlocBuilder on the home screen which in turn never allows the ListView builder to build the buttons. This is in spite of the fact that I've provided the Bloc very high up on the Widget tree.

I Have attached my project.
cntl_app.zip

I will really appreciate your feedback as I've
been trying to figure this out for quite some time

Thanks!

Hi @kieronwiehahn ๐Ÿ‘‹
Thanks for opening an issue!

The problem is you're creating multiple instances of the RoomCardsBloc. You create one above MaterialApp in main.dart on line 29 and then you create a different instance in main.dart on line 45 so your HomeScreen is always listening for changes in the second one but you are adding events to the first one. Just remove the second one and you should be good.

            return BlocBuilder<AuthenticationBloc, AuthenticationState>(
              builder: (context, state) {
                if (state is Authenticated) {
                  return HomeScreen();
                  // You don't need to create multiple instances of RoomCardsBloc
                  // return BlocProvider<RoomCardsBloc>(
                  //   create: (context) => RoomCardsBloc(
                  //     roomCardsRepository: const RoomCardsRepositoryFlutter(
                  //         fileStorage: const FileStorage(tag: 'test')),
                  //   ),
                  //   child: HomeScreen(),
                  // );
                }
                if (state is Uninitialized) {
                  return SplashScreen();
                }

Hope that helps! ๐Ÿ‘

LOL OMG thanks so much! I miss understood what you were doing by using the second MultiblocProvider after Authenticating the user. I thought I had to Reinject the bloc along with others at that point in order for it to see the RoomCardsBloc. Thank you. I made that change and it immediately worked lol