Marcin Wróblewski
Marcin Wróblewski

Reputation: 1247

How to debounce events in bloc?

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);

Upvotes: 3

Views: 2746

Answers (1)

Marcin Wr&#243;blewski
Marcin Wr&#243;blewski

Reputation: 1247

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)),
);

Upvotes: 6

Related Questions