Hey Felix,
Sorry if this has been suggested before - I did a quick search through the issues and didn't find anything.
So, I was just thinking it might be convenient to do something like
builder: (context, state, event) {}
so we can add events with event.add(EventName()); without having to do the Bloc bloc = BlocProvider.of<Bloc>(context); and then calling bloc.add(EventName()) each time.
The reason I think this would be convenient is because I frequently need access to event.add whenever I'm inside of BlocBuilder. Like calling onTaps, onPressed and such.
Thanks
P.S. I'm loving cubit - i use it in conjunction with Bloc and it's done wonders for my codebase.
Hi @theweiweiway 馃憢
Thanks for opening an issue and for the positive feedback!
I believe this is a duplicate of https://github.com/felangel/bloc/issues/447.
I prefer not to add this because in cases where it's not necessary to add events from within the BlocBuilder you will still be forced to pass another argument. Also in order to support this properly and enforce strict types we will need to specify the event type as well:
BlocBuilder<MyBloc, MyState, MyEvent>(
builder: (context, state, event) {
return RaisedButton(
onTap: () => event.add(...),
);
},
)
This makes the API a lot more verbose as opposed to:
BlocBuilder<MyBloc, MyState>(
builder: (context, state) {
return RaisedButton(
onTap: () => context.bloc<MyBloc>().add(...),
);
},
)
In my opinion, we're not really gaining much in cases where it would be used and we're complicating the API in all cases. Thoughts?
I see, yeah the extra <MyEvent> is definitely a big turn-off..
Would there be some sort of way to make the third <MyEvent> and event argument optional?
Unfortunately, this is a Dart limitation. Check out https://github.com/dart-lang/language/issues/620 for the open issue. We could theoretically expose the bloc itself rather than an "event-adder" function which would eliminate the need to specify the event type but again I feel it's a bit strange to expose the bloc as part of the builder.
What is the main issue with using context.bloc to lookup the bloc and add the event as needed? The lookup is O(1) and using context.bloc is fairly concise imo.
I see - there's no issue to look it up with context.bloc. You are right, it is concise.
OK - closing this, thanks Felix
Most helpful comment
I see - there's no issue to look it up with
context.bloc. You are right, it is concise.OK - closing this, thanks Felix