如何为 'onEvent' 的事件实施去抖动器?

how to implement debouncer for events with 'onEvent'?

transformEvents 方法将在 bloc 版本 8 中删除,我们应该使用 onEvent 方法代替,我们如何为带有 onEvent 的事件实现 debounce

  @override
  Stream<Transition<PriceProposalEvent, PriceProposalState>> transformEvents(
    Stream<PriceProposalEvent> events,
    TransitionFunction<PriceProposalEvent, PriceProposalState> transitionFn,
  ) =>
      super.transformEvents(
        events.debounceTime(const Duration(milliseconds: 200)),
        transitionFn,
      );

rxdart 库用于我看到您已经在使用的流方法:

static Stream<T> debounce<T>(
    Stream<T> events,
    Stream<T> Function(T) transitionFn,
) {
    return events
        .debounceTime(const Duration(milliseconds: 200))
        .switchMap(transitionFn);
}

Bloc 7.2.0 中的新功能https://verygood.ventures/blog/whats-new-in-bloc-v7-2-0

现在使用 transformer!

import 'package:bloc/bloc.dart';
import 'package:stream_transform/stream_transform.dart';

class YourBloc extends Bloc<Event, State> {
  YourBloc() : super(StateInitial()) {

    on<PriceProposalEvent>(_onPriceProposalEvent,
        transformer: debounce(const Duration(milliseconds: 200)));
  }
}
//Debounce query requests
EventTransformer<E> debounce<E>(Duration duration) {
  return (events, mapper) {
    return events.debounce(duration).switchMap(mapper);
  };
}

希望对你有所帮助!