【问题标题】:how to implement debouncer for events with 'onEvent'?如何使用“onEvent”为事件实现去抖动器?
【发布时间】:2021-11-21 03:11:46
【问题描述】:

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

【问题讨论】:

    标签: flutter dart bloc flutter-bloc


    【解决方案1】:

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

    希望对你有帮助!

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 感谢您回答这个问题,但我正在寻找带有onEvent 回调的解决方案。
      猜你喜欢
      • 2021-03-10
      • 2021-03-07
      • 2021-09-08
      • 1970-01-01
      • 2019-12-31
      • 1970-01-01
      • 2015-04-12
      • 1970-01-01
      相关资源
      最近更新 更多