flutter - 如何在 flutter/Dart block 中取消 'await for'

标签 flutter asynchronous dart

我正在使用流读取块中的位置数据。我有一个开始和一个停止事件。在 stop 方法中,我取消了流订阅。当我使用 listen到一个流到 yield yield所在的状态语句永远不会被调用。

  Stream<LocationState> _start() async* {

    _locationSubscription = location.onLocationChanged.listen(
      (location) async* {
        if (location.isNotNull) {
          yield LocationState.sendData(location: updateLocation(location));
        }
      },
    );

    //send one initial update to change state
    yield LocationState.sendData(
        location: updateLocation(await Location().getLocation()));
  }

 Stream<LocationState> _stop() async {
    await _locationSubscription?.cancel();
    _locationSubscription = null;
    yield LocationState.stoped();
 }
当我更换 listenawait for我看不到任何方式
阻止它产生事件,因为订阅句柄消失了。
有任何想法吗?有什么解释吗?
   Stream<LocationState> _start() async* {


    await for (LocationData location in location.onLocationChanged) {
      if (location.isNotNull) {
        yield LocationState.sendData(location: updateLocation(location));
      }
    }

    //send one initial update to change state
    yield LocationState.sendData(
        location: updateLocation(await Location().getLocation()));
  }

最佳答案

问题是我完全不理解yield的行为。
此外,dart 框架也有一些缺点。
与 Dart 制造商详细讨论了这个问题,这里。
https://github.com/dart-lang/sdk/issues/42717
和这里
https://github.com/felangel/bloc/issues/1472

关于flutter - 如何在 flutter/Dart block 中取消 'await for',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62926257/

相关文章:

javascript - 减少来自客户端的第三方异步调用次数

javascript - 将值传递给nodejs中的回调

dart - 路径 quadraticBezierTo : curve pass the control point

flutter - 在拍打状态下显示警报

flutter - 如何在 Flutter 按 View 模型中正确组织 API 调用?

flutter - 如何在 flutter 中改变盒子阴影的高度、宽度和不透明度?

javascript - 无法访问 Controller 中的 $scope 变量

text - 如何从 Flutter TextBox 中获取原始文本

android - 有办法在Flutter中缓存卡吗?

flutter - 文本字段 flutter 的默认值?