How to debounce events in bloc? Let's say, I want to perform a search query on a backend, but before doing that wait for the user to stop typing? By "stop typing" I mean 1 second period until last key was pressed.
Example bloc event handler:
on<Search>(_handleSearchEvent);
Each handler have an optional transformer:
field which can do debouncing (and much more).
Using rxdart you can implement debouncing yourself:
on<Search>(
_handleSearchEvent,
transformer: (events, mapper) => events.debounceTime(Duration(seconds: 1)).switchMap(mapper),
);
I wrote the bloc_event_transformers package to do popular transforms like throttle and debounce to reduce the boilerplate in my apps. It can be used like that:
on<Search>(
_handleSearchEvent,
transformer: debounce(Duration(seconds: 1)),
);