I'm making a search function that calls events every TextFormField
Onchanged and trying to add debouncer to limit its api calls. I'm trying this code from this link. When I type and wait for 1 second its working. But when I continue to add another letter nothing happens. It doesn't even trigger the mapEventToState
and transformEvents
function when I added a break to it. I'm using flutter_bloc: ^6.1.3
TextFormField:
TextFieldTemplate(
onChanged: (value)
{
search = _searchController.text;
BlocProvider.of<ListBloc>(context).add(
SearchListEvent(
search: search,
limit: PAGE_SIZE,
offset: pageKey));
},
),
Bloc:
@override
Stream<Transition<ListEvent, ListState>>
transformEvents(Stream<ListEvent> events, transitionFn) {
final defferedEvents = events
.where((e) => e is SearchListEvent)
.debounceTime(const Duration(seconds: 1))
.distinct()
.switchMap(transitionFn);
final forwardedEvents = events
.where((e) => e is! SearchListEvent)
.asyncExpand(transitionFn);
return forwardedEvents.mergeWith([defferedEvents]);
}
@override
Stream<ListState> mapEventToState(ListEvent event,) async* {
if (event is SearchListEvent) {
yield ListLoadInProgressState();
final failureOrSearch = await getProduct(Params(
search: event.search,
offset: event.offset,
limit: event.limit,
));
yield* _eitherLoadedOrErrorState(failureOrSearch);
}
}