-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
UI doesn't react to state changes after using Navigator.push #4156
Comments
I have the same problem and I couldn't find the solution. 🤔🤔🤔 |
I've had the same problem for about 2 weeks and I still haven't resolved it... |
This is not an issue with Bloc but with your design. You have your bloc above Imagine routes/pages as "papers" and widgets as "drawings" on that paper. Based on what you described, your pages are Home Page and Second Page. On page Home there might be "drawings" welcome or first. That means when you are on Home Page, you can actually see bloc builder to build something. But when you navigate to Second Page, again imagine a paper, you cannot see changes on Home Page, but only your current page, no matter if Home Page actually still exists or not. So if you want to create navigation based on states, you need to choose different approach, since your does not do that. I would recommend i.e. creating states based on auth state like Use BlocListener under provider (you may need Builder above BlocListener to provide current context) and listen to changes there and there you can redirect to different pages (display different "papers"), or you can use something like GoRouter package which I recommend anyway and MyStates? lastState;
FutureOr<String?> handleRedirect(
BuildContext context,
GoRouterState state,
) {
final appState = context.read<MyBloc>().state;
// Same route.
final isSameState = appState == lastState;
if (isSameState) {
return null;
}
lastState = appState;
// Auth in progress.
if (appState is AuthInProgress) return null;
if (appState is UserNeedsToLogin) {
return '/login';
}
return null; I hope you understand the concepts why your solution couldnt do that thing you wanted. Feel free to ask more questions for sure. :) |
Thank you very much for your early answer. So, I think that in conclusion is not a good idea to think about screens as if they were states. I'm currently redesigning my app. |
Hello, I'm new in Flutter and I'm still learning state management with BLoC. I have a problem when I use Navigator.push. It's like the "context" of the BLoC is getting lost.
And finally we arrive to my problem. When I press the button in the second page, through some prints I can see that the state is effectively changing to "WelcomePage", but the app doesn't return "Welcome" it stays showing SecondPage.
I have tested that if I add a
Navigator.pop()
aftercontext.read<MyBloc>().add(const UserThatHasToLogInDetected());
in the SecondPage then it returns to the FirstPage and then it responds to the state change returning "Welcome".Of course in my real code I have much more states, events and lines of code. But I wanted to explain my problem briefly. Thanks in advance.
The text was updated successfully, but these errors were encountered: