CubitListener behaviour skips(1) #36
Comments
Hi @Manuelbaun 👋 The listener is by design implemented in such a way that it will only react to state changes after it is mounted into the tree, since it doesn't make sense to react to the last known state since that might be obsolete by the time the listener subscribes to the stream of states. Your issue raises from your architecture. Your Hope it helps 👍 |
HI @RollyPeres, Thanks for pointing it out. I will look into the example you've posted. Since I am also using the Auto_route package, I will have to look at how I can make it work together. |
I looked into the example and I could refactor authentication as you suggested and provided it to the builder in the MaterialApp. But the issue remains: But currently, there is no way, to find out, when the CubitListener is mounted to the tree. So if call the I could use a time delay, but it a dirty hack and potential for a race condition. Do you have an idea of how I can manage that? |
Hi @Manuelbaun 👋 Cubit is still async and this isn't specific to cubit (you can have a race-condition with listeners with bloc as well). I would highly recommend lifting your listener up in the widget tree to be above where the method is called. That guarantees the listener is mounted before anything has happened. If you're still having trouble, it would be great if you could share a link to a sample github repo which illustrates the problem you're having. Thanks! 👍 |
I've created a quick sample to illustrate this common request, maybe others will also find it useful. You can check it out here 🎉 |
Hi Felix,
I have a question to the CubitListener: Why does the subscribe method skips the first state?
I am using an AuthCubit and AuthState with freezed like here
and I got a Splashscreen Widget like here:
So now to my Problem:
When I listen to the Cubit with CubitListener, the CubitStream first yields the current state, then it will yield all controller stream states. But in the CubitListener, the first yield will be skipped and this causes the problem, that the listener method of the CubitListener will not be called when the widget is built for the first time.
In this case, I am not able to either navigate to the dashboardPage, if the state is 'Authenticated' nor to the signInPage if I am 'Unauthenticated'.
Obviously, if the widget is built and I emit a state not equal to the last state then, then the listener method is called, and I can to whatever I need.
Maybe, we could have an option, if we want to skip the first yielded state?
The text was updated successfully, but these errors were encountered: