-
-
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
Bad state: Cannot emit new states after calling close #3756
Comments
Hey @Lazizbek97, could you send an example of how you setup your bloc provider and the |
I am not able to run void main for test cases as it is showing Bad state: Cannot add new events after calling close.. _dart:async _BroadcastStreamController.add Bad state: Cannot add new events after calling close_ |
Hey @sanya-wadhwa, could you send us a minimal reproducible example with the error? |
I'm also encountering the same issue. In my implementation, I have a widget tree with a Screen widget that contains a MultiBlocProvider with a CustomTabBarView as its child. Each CustomTabBarView has some blocs that are necessary for it to works properly. In the dispose method of the widget, I add a new event to the blocs to perform some necessary cleanup. However, when attempting to add an event to another bloc inside one of the blocs, a StateError is thrown because the event stream appears to be closed. This is problematic because the isClosed property of the bloc always returns false, as it only checks the StateStream and not the eventStream. My Widget Tree:
|
Well sounds more like issues due to improper use. Either move blocs above the tab bar so they stay alive even when you switch tabs or use Send us a minimal reproducible example with the error you are facing so we can determine the exact issues. |
Closing for now since there is no minimal reproduction sample. Feel free to provide one and we're happy to take a closer look, thanks! 👍 |
Simple Project to reproduce : https://github.com/farukprogrammer/bloc_8_1_2_issue The crash happen when there is still some process that will emit when finish, but the user is closing the screen first. |
I have same issue, do you have any solution? |
@farukprogrammer but this is expected as you dispose the route when you pres back button and therefore all of it's widgets as well, resulting in closing the bloc and this error if you emit something after it being closed. Ideally you should check before emiting whether the bloc is still open or not. if (isClosed) return;
emit(...); However I agree that this could be done in the library itself and produce just some debug log (?) when this happends, since "almost everyone" I know has some kind of Of course, ideally, making cancelable async operations would be better. #3069 |
hi @tenhobi thx for the feedback. But you see, every year, there will be thousands of new programmer that learn flutter. And when it goes to productions, I don't know how many users will be impacted, and we will responsible for that. Nahh maybe not. |
I understand and in general I agree. But the same you have with Widgets and their context maybe not being mounted. It's just part of the design and reality. I am not entirely sure that not producing exceptions (which don't stop your program btw) and basiclly hiding wrong-usage that way would in fact benefit more than curent state. Because now you at least are super sure that issues in bloc logic get to you. |
I'm sorry @tenhobi what do you mean by wrong usage? the way we handle it is by not emit when it's closed, which is should be handled by bloc, and it handled in previous versions. Make it crash instead of handling it inside bloc, make many programmer life getting more difficult, not sure if programmer have life though. |
This isn't a crash -- the app will still continue to function correctly. It's an error indicating that the developer didn't correctly check if the bloc was closed before emitting a new state and it is consistent with how primitives like Closing this for now since it's been a while but if this is still an issue let me know and I'm happy to continue the discussion 👍 |
Description
I've been using Cubit&Freezed for the app state management. I have wrapped MaterailApp with MultiBlocProvider and initialized all providers there. When the user logout, the app should remove all pages and push them to the login page. Here is the Problem. My main cubit is being disposed when i navigate with pushAndRemoveUntill() to login page, and after user login again, the app crashes with the error:
Expected Behavior
When the user logout and login, I want to refresh all cubits according to the user's credentials. without any crashes.
The text was updated successfully, but these errors were encountered: