I think bloc builder is good when it comes to rebuilding widget. I just want to say that I feel it is a bit boilerplatey especially in the Generics part, buildWhen method and the builder method part. First, I just think that can we eliminate the bloc/cubit generic since I only wanted the state? Second, I think this "previousState.count != currentState.count" condition is the most common condition we can have in buildWhen that can be easily be replaceable with the propose feature below. For more complex condition I think its suitable to use BlocBuilder Widget.
class CounterText extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocBuilder<CounterCubit, CounterState>(
buildWhen: (previousState, currentState) => previousState.count != currentState.count,
builder: (context, state) {
final count = state.count;
return Text(
'$count',
style: Theme.of(context).textTheme.headline4,
);
},
);
}
}
I think this easily manageable code when we have to many widgets with the same pattern unlike above? This should only rebuild when only the state.count changes and will not be affected by other state changes.
class CounterText extends StatelessWidget {
@override
Widget build(BuildContext context) {
final count = context.blocSelect((CounterState state) => state.count);
return Text(
'$count',
style: Theme.of(context).textTheme.headline4,
);
}
}
Underlying Signature
extension BlocSelectContext on BuildContext {
R blocSelect<T, R>(R selector(T value)) {}
}
Hi @ianjaspersantos 馃憢
Thanks for opening an issue!
Unfortunately we cannot remove the bloc/cubit generic because without it the BlocBuilder wont' be able to do the lookup for you (BlocProvider.of<T>(context)).
I have been thinking about having a selector and will spend some time on it once I wrap up the documentation updates for v6.0.0 馃槃
Thanks Fel! Thank you for all the effort making flutter coding as great as possible 馃榿
Selectors would be a way to solve #1512 even nicely 馃檶
Most helpful comment
Hi @ianjaspersantos 馃憢
Thanks for opening an issue!
Unfortunately we cannot remove the bloc/cubit generic because without it the
BlocBuilderwont' be able to do the lookup for you (BlocProvider.of<T>(context)).I have been thinking about having a selector and will spend some time on it once I wrap up the documentation updates for v6.0.0 馃槃