felangel / bloc

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

Subscription Dispose #2142

Closed jurej1 closed 3 years ago

jurej1 commented 3 years ago

This error report address the issue on not closing the stream subscription.

I went thru the project https://github.com/felangel/Bloc/tree/master/examples/flutter_firestore_todos (it is a firebase todos app). Basically I can not figure out, since you are creating multiple stream subscriptions inside of the blocs ( FilteredTodosBloc, Stats, Todos...). I can not figure it out where are you closing them.

Our is it enough for stream subscription to be closed when you simply overwrite the close method inside of a bloc?

SEGVeenstra commented 3 years ago

I think that if you use Blocs (or other streams inside your own Bloc you have to close them in the close method of your bloc. When you create a Bloc in the create method of a BlocProvider I thought that this will handle the closing for you.

jurej1 commented 3 years ago

Thanks a lot. Because I have saw different tutorial talking about it when you like create a subscription inside of a bloc/cubit that you also have to close it manually. So when they say you have to close it manualy they are refering to the close method inside of a bloc/cubit ?

SEGVeenstra commented 3 years ago

Probably, yes.

See: https://bloclibrary.dev/#/architecture?id=bloc-to-bloc-communication

felangel commented 3 years ago

Hi @jurej1 👋 Thanks for opening an issue!

As @SEGVeenstra mentioned, if you create subscriptions within the bloc then you cancel them in the close override 👍

Closing for now but feel free to comment with additional questions and I'm happy to continue the conversation 😄