收到新事件时,Flutter Bloc取消跑步活动

发布于 2025-01-21 04:29:35 字数 783 浏览 1 评论 0原文

当FetchDevicEseVent事件触发时,我有follwing集团

class DeviceBloc extends Bloc<DeviceEvent, DeviceState> {
  DataRepository repository;

  DeviceBloc({@required this.repository}) : super(DeviceInitialState()) {
    on<FetchDevicesEvent>(onFetchResources);
  }

  Future<void> onFetchResources(
      FetchDevicesEvent event, Emitter<DeviceState> emit) async {
    emit.call(DeviceLoadingState());

    try {
      List<DeviceResource> devices = await repository.getResources(event.type);
      emit.call(DeviceLoadedState(devices: devices));
    } catch (e) {
      emit.call(DeviceErrorState(message: e.toString()));
    }
  }
}

,如果在运行任务完成之前收到了其他FetchDevicEsevent事件,则会开始长期运行的任务。我该如何暂停已久的任务,并在收到新的Fetchdevicesevent后始终立即开始新的任务?

I have the follwing Bloc

class DeviceBloc extends Bloc<DeviceEvent, DeviceState> {
  DataRepository repository;

  DeviceBloc({@required this.repository}) : super(DeviceInitialState()) {
    on<FetchDevicesEvent>(onFetchResources);
  }

  Future<void> onFetchResources(
      FetchDevicesEvent event, Emitter<DeviceState> emit) async {
    emit.call(DeviceLoadingState());

    try {
      List<DeviceResource> devices = await repository.getResources(event.type);
      emit.call(DeviceLoadedState(devices: devices));
    } catch (e) {
      emit.call(DeviceErrorState(message: e.toString()));
    }
  }
}

When FetchDevicesEvent event is triggered it starts a long running task, if additional FetchDevicesEvent events are recieved before the running task is completed the wrong result are returned to the caller. How can I suspend the awaited task and always start a new as soon as a new FetchDevicesEvent is recieved?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅听莫相离 2025-01-28 04:29:35

通过使用transformer:Bloc_concurrency软件包的Res​​tartable()我自己找到解决方案

class DeviceBloc extends Bloc<DeviceEvent, DeviceState> {
  DataRepository repository;

  DeviceBloc({@required this.repository}) : super(DeviceInitialState()) {
    on<FetchDevicesEvent>(
      onFetchResources,
      transformer: restartable(),
    );
  }

  Future<void> onFetchResources(
      FetchDevicesEvent event, Emitter<DeviceState> emit) async {
    emit.call(DeviceLoadingState());

    try {
      final List<DeviceResource> devices =
          await repository.getResources(event.type);
      emit.call(DeviceLoadedState(devices: devices));
    } catch (e) {
      emit.call(DeviceErrorState(message: e.toString()));
    }
  }

}

Found the solution myself by using transformer: restartable() from bloc_concurrency package

class DeviceBloc extends Bloc<DeviceEvent, DeviceState> {
  DataRepository repository;

  DeviceBloc({@required this.repository}) : super(DeviceInitialState()) {
    on<FetchDevicesEvent>(
      onFetchResources,
      transformer: restartable(),
    );
  }

  Future<void> onFetchResources(
      FetchDevicesEvent event, Emitter<DeviceState> emit) async {
    emit.call(DeviceLoadingState());

    try {
      final List<DeviceResource> devices =
          await repository.getResources(event.type);
      emit.call(DeviceLoadedState(devices: devices));
    } catch (e) {
      emit.call(DeviceErrorState(message: e.toString()));
    }
  }

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文