@felangel great stuff. Most examples though cover quite simple data relationships, e.g. a list and a filter on that list, or a list and an aggregate on that list. I'm not always sure however where the pieces go with data joins e.g. Product-Buyer-Order.
In the below, I'd like to rebuild UI on Foo changes and reference data from Bar in doing so. Would you say this looks about right?
@override
Widget build(BuildContext context) {
var fooBloc = BlocProvider.of<FooBloc>(context);
var barBloc = BlocProvider.of<BarBloc>(context);
var barState = barBloc.currentState;
return BlocBuilder<BlocEvent, BlocState>(
bloc: fooBloc,
builder: (BuildContext context, BlocState fooState) {
if (fooState is FooUpdated && barState is BarUpdated) {
// use fooState and barState to build UI
}
});
}
If I wanted to update UI given changes in either Foo or Bar, I'd presumably need to nest BlocBuilders, e.g. would this look about right?:
@override
Widget build(BuildContext context) {
var fooBloc = BlocProvider.of<FooBloc>(context);
return BlocBuilder<BlocEvent, BlocState>(
bloc: fooBloc,
builder: (BuildContext context, BlocState fooState) {
if (fooState is FooUpdated) {
var barBloc = BlocProvider.of<BarBloc>(context);
return BlocBuilder<BlocEvent, BlocState>(
bloc: barBloc,
builder: (BuildContext context, BlocState barState) {
if (barState is BarUpdated) {
// use fooState and barState to build UI
}
});
}
});
}
Or should instead I always go to the trouble of creating a FooBarBloc and combining both Foo and Bar state streams with e.g. Observable.combineLatest2(), along with a whole new set of FooBar event classes and FooBar state classes? Would both approaches (i.e. nested BlocBuilders and a new FooBarBloc) effectively amount to the same thing, outside of potential reusability of FooBarBloc?
Thanks for any comments/advice RE: above in particular and presumably quite common data-joining scenarios with BLoC in general much appreciated.
Hey @brent-williams 馃憢
Thanks for the positive feedback and for opening an issue!
Regarding your questions,
Let me know if that helps and great question 馃憤
Closing for now but feel free to comment with additional questions and I'm happy to continue the conversation 馃憤
@felangel Nested BlocBuilder doesn't honor condition .
My use case:
Player widget -> Show only when the player is playing
The Player Widget has play , pause, stop, title, progress bar, timer,
I don't want the player widget get redrawn completely for event change in timer, to update only the timer. How do I nest it with condition with single Bloc
@vjyanand the condition on BlocBuilder only comes into play when the bloc's state changes. There are many other reasons for BlocBuilder to rebuild such as if the parent widget rebuilds.
Have you taken a look at the timer tutorial? It includes how to add a condition to optimize for rebuilds towards the end of the actions section.
Hope that helps 馃憤
Yes, but nested blocbuilder doesn鈥檛 work as expected with condition
Most helpful comment
Hey @brent-williams 馃憢
Thanks for the positive feedback and for opening an issue!
Regarding your questions,
Let me know if that helps and great question 馃憤