Flutter – Transform flutter bloc event to add debounce

Issue

I am trying to make an Infinite list using flutter bloc.

I have taken reference from here: https://github.com/felangel/bloc/blob/926029cae2d7614d38b5a9a8952e36bb59054b02/examples/github_search/common_github_search/lib/src/github_search_bloc/github_search_bloc.dart#L15

According to this post, to add a delay between two events so that api doesn’t get spammed, you need to override transformEvents and add debounce to events like this:

@override
  Stream<GithubSearchState> transformEvents(
    Stream<GithubSearchEvent> events,
    Stream<GithubSearchState> Function(GithubSearchEvent event) next,
  ) {
    return (events as Observable<GithubSearchEvent>)
        .debounceTime(
          Duration(milliseconds: 300),
        )
        .switchMap(next);
  }

Problem I am facing is that the Observable has been deprecated by RxDart and I am not sure how to accomplish the above requirement.

Solution

I found the solution.

@override
  Stream<Transition< GithubSearchEvent, GithubSearchState >> transformEvents(
      Stream< GithubSearchEvent > events, transitionFn) {
    return events
        .debounceTime(const Duration(milliseconds: 300))
        .switchMap((transitionFn));
  }

Answered By – Suhas Shelar

Answer Checked By – Jay B. (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *