Is your feature request related to a problem? Please describe.
Currently in BlocObserver we have onEvent, onChange, onTransition and onError
These are the things that happen throughout the Bloc's lifecycle.
However we don't know when the Blocs actually get created and get disposed.
Describe the solution you'd like
It would be SUPER helpful if we had something like onCreate and onDispose in BlocObserver Api.
Additional context
My blocs are created inside of a ListView.builder (lazily I guess)
That's why I want to keep track of the lifecycle of my blocs. Because I don't know what ListView is doing with it's children internally once they are farther away from the viewport.
I am profiling my app for memory leaks and I need to follow if I dispose my blocs(I have a lot) properly.
This feature will not result in a breaking change since if somebody does not need these 2 callbacks, they will simply not override them.
Do you all think this is a good idea and if it is doable? Thanks once again
Hi @aytunch 馃憢
Thanks for opening an issue!
Can you provide some more information regarding why you need this in the BlocObserver? Couldn't you override close on your blocs as well as the constructor to know when they are created and disposed?
class MyBloc extends Bloc<MyEvent, MyState> {
MyBloc() : super(MyState.initial()) {
// onCreate: do something
}
@override
Future<void> close() {
// onClose: do something
return super.close();
}
}
Hi @felangel 馃憢
I already override close() in all of my Blocs since I have network subscriptions to cancel in nearly all of them.
Being able to see the creation and disposing of all the blocs from BlocObserver would be very convenient.
Afaik onEvent can easily be tracked inside of a Bloc's mapEventToState too. Similarly, onTransition while we yield a new state.
I seeBlocObservers behaviour as a Central Hub to access changes easily especially for debugging purposes. And creation and disposing/closing are as important as other bloc callbacks imho.
For my particular case, I have a Bloc per a ListView.builder's ListItem
ListView is responsible for creating, reusing and disposing the ListItem widgets and along with these widgets, Blocs are getting created and disposed. Right?
If I can track with BlocObserver which bloc is killed or created, I can use this info for making smarter/more efficient decisions on preparing and saving/loading the data I am getting from network.
@aytunch thanks for the clarification! I don't see an issue with adding them for debugging/logging purposes 馃憤
Thanks a lot. This was lightning quick 馃憤
So will we able to try it with flutter_bloc 6.0.6?
@aytunch it's available in bloc v6.1.0 馃憤
@felangel Hi, is onCreate and onClose only for Cubits? I am using regular Blocs. Can i see them in these callbacks as well?
/// Called whenever a [Cubit] is instantiated.
/// In many cases, a cubit may be lazily instantiated and
/// [onCreate] can be used to observe exactly when the cubit
/// instance is created.
@aytunch a bloc is also a cubit so it will work for both 馃憤
@aytunch a bloc is also a cubit so it will work for both 馃憤
Phew. Thats a BIG relief :D