Is your feature request related to a problem? Please describe.
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
Is there any documentation or recommendation for communication between blocs beside having dependencies between them? The point is to keep blocs independent of each other and use streams to allow them to communicate with each other.
While we can use the BlocDelegate to trigger certain blocs to do certain things, but obviously this is not the best solution.
Describe the solution you'd like
A clear and concise description of what you want to happen.
There should be a mechanism for one bloc to listen to another bloc by registering a handler to handle when a state change in that bloc.
Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.
Additional context
Add any other context or screenshots about the feature request here.
@clicksocial thanks for opening an issue for this. If you don't want a bloc to have a direct dependency on another bloc you can always inject the stream that you want the bloc to react to.
For example, if we want BlocA to react to state changes in BlocB, we can always do something like:
class BlocA extends Bloc<BlocAEvent, BlocAState> {
final Stream<BlocBState> stateStream;
StreamSubscription stateSubscription;
BlocA(this.stateStream) {
stateSubscription = stateStream.listen((state) {
if (state is ...) {
// do something
}
});
}
...
@override
dispose() {
stateSubscription.cancel();
super.dispose();
}
}
We can then pass the state stream of BlocB into BlocA like:
final blocA = BlocA(blocB.state);
I would also recommend looking at the Todos Application for a more concrete example (FilteredTodosBloc
reacts to state changes in TodosBloc
).
Does that answer your question? 馃憤
Hi @felangel ,
This looks like its going to work. I'll give it a try.
Thanks for your timely response!
Quan
@clicksocial no problem! Closing this for now but feel free to comment if you're having trouble or have additional questions 馃憤
Most helpful comment
@clicksocial thanks for opening an issue for this. If you don't want a bloc to have a direct dependency on another bloc you can always inject the stream that you want the bloc to react to.
For example, if we want BlocA to react to state changes in BlocB, we can always do something like:
We can then pass the state stream of BlocB into BlocA like:
I would also recommend looking at the Todos Application for a more concrete example (
FilteredTodosBloc
reacts to state changes inTodosBloc
).Does that answer your question? 馃憤