felangel / bloc

A predictable state management library that helps implement the BLoC design pattern
https://bloclibrary.dev
MIT License
11.71k stars 3.38k forks source link

fix: Bloc listener is not listening after cubit emit new state #4219

Open kjhamja77 opened 1 month ago

kjhamja77 commented 1 month ago

Description The Bloc listener is not listening for changes from the Authentication cubit but the Bloc builder got a new state and updated the UI based on the new state emitted from the cubit.

Steps To Reproduce

  1. Authentication cubit is set first with the init function that emits a new state based on the user ID, we store the user ID while logging, if the user ID is null, navigate to the login screen, if the user ID is not null, navigate to the home page.
  2. Gave bloc provider in the main function of my app class, and in my app class we defined the route in on generate route and home with the class in which we use bloc listener and bloc builder
  3. Bloc Builder got the state and it shows the UI based on the state, but the Bloc listener doesn't.

Expected Behavior I need to navigate to the home page if the user ID is not null, else I need to navigate to the login screen.

Screenshots kindly check the below screenshots Screenshot 2024-07-30 130718 Screenshot 2024-07-30 130352 Screenshot 2024-07-30 130313

tenhobi commented 3 weeks ago

It's hard to judge since you didn't show what is on the first screenshot and when in the widget tree you are using it. But my guess would be that you are expecting the widget with BlocListener and BlocBuilder (you can combine them using BlocConsumer btw) to work with the latest state of that bloc/cubit. But that is only true for the BlocBuilder, not for BlocListener. BlocListener works only with state changes, aka you must have the widget attached before the state changes to trigger the listener.

nkalahanov commented 3 weeks ago

You already provided cubit for the whole app, in your case, you don't need to provide the same cubit again, as we see per the first screenshot.