GithubHelp home page GithubHelp logo

Comments (5)

narcodico avatar narcodico commented on May 18, 2024 3

I've created a quick sample to illustrate this common request, maybe others will also find it useful.

You can check it out here 🎉

from cubit.

Manuelbaun avatar Manuelbaun commented on May 18, 2024 1

HI @RollyPeres,
yes, you are right, it makes totally sense now. I also found out, that when I would change the skip to 0, then I get another undesigned behavior, in the sign-in/signout flow. So it must be some architecture-related issues.

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.

from cubit.

felangel avatar felangel commented on May 18, 2024 1

Hi @Manuelbaun 👋
Thanks for opening an issue!

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! 👍

from cubit.

narcodico avatar narcodico commented on May 18, 2024

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 CubitListener<AuthCubit, AuthState> should be placed globally instead of scoping it to your splash screen, because you always want it to be ready to react to state changes in your authentication.
A good place to provide it would be inside MaterialApp's builder.
You can check out this example and adapt it to cubit.

Hope it helps 👍

from cubit.

Manuelbaun avatar Manuelbaun commented on May 18, 2024

I looked into the example and I could refactor authentication as you suggested and provided it to the builder in the MaterialApp.
However, the example uses bloc with the async event dispatch, but Cubit uses methods (sync).

But the issue remains:
I need to call the method checkIfAuthenticated of the AuthCubit after the CubitListener listens to the AuthCubit. Then the listener would be triggered properly and I can navigate to another page.

But currently, there is no way, to find out, when the CubitListener is mounted to the tree. So if call the checkIfAuthenticated method just before its mounted, then I will not be able to navigate to another page.

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?

from cubit.

Related Issues (18)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.