【问题标题】:How to work with async* functions in Dart如何在 Dart 中使用 async* 函数
【发布时间】:2020-09-23 15:24:32
【问题描述】:

我正在使用 flutter_bloc 库。

在 bloc 中,mapEventToState 方法是一个 async* 函数,它返回 Stream<BlocState>。 从这个函数中,我正在调用其他 async* 这样的函数 yield* _handleEvent(event)

在这种方法中,我调用了一些 Future 返回函数,但在 Future then() 函数中它不会让我调用其他 yield* 函数。

这是一个例子:

Stream<BlocState> mapEventToState(BlocEvent event) async*{
     yield* _handlesEvent(event); //This calls to worker method 
}

Stream<BlocState> _handleEvent(BlocEvent event) async* {
   _repository.getData(event.id).then((response) async* { //Calling Future returned function
         yield* _processResult(response); //This won't work
     }).catchError((e)  async* {
         yield* _handleError(e);  //This won't work either
     });

   Response response = await _repository.getData(event.id); //This do works but I want to use it like above, is it possible?
   yield* _processResult(response); //This do works
}

然而,问题是,如何在 dart 中结合 Future 和 Stream 。 我可以使用await _repository.getData 有效。但是我不会发现错误。

【问题讨论】:

  • 使用简单的await 而不是then: var response = await _repository.getData(event.id); yield* _processResult(response);
  • 谢谢,我在我的问题中提到了这一点,我该如何捕捉这样的错误?我想使用 Future api 的内置 catch 错误。
  • 使用普通的 try-catch - 什么不起作用?
  • 谢谢,请参考下面我对@Candace 的回复

标签: flutter dart dart-async


【解决方案1】:
  1. await 只是.then() 的语法糖,将await 放入try-catch 块是使用.catchError 的语法糖。一种方式可以做的事情可以用另一种方式完成。

  2. 在您使用 .then()/.catchError() 的第一个版本中,您的函数不会返回任何内容。

  3. 您的回调不起作用,因为您在其中使用了yield*,但您尚未使用sync* 或async* 指定回调。为避免名称冲突,yield 关键字需要它们(就像await 需要函数一样,使用async 或async*)。

这是一个应该与.then() 和.catchError() 一起使用的版本:

Stream<BlocState> _handleEvent(BlocEvent event) async* {
  yield* await _repository.getData(event.id).then((response) async* {
    yield* _processResult(response);
  }).catchError((e) async* {
    yield* _handleError(e);
  });
}

注意回调不需要使用yield*;他们可以直接返回他们的Streams:

Stream<BlocState> _handleEvent(BlocEvent event) async* {
  yield* await _repository.getData(event.id).then((response) {
    return _processResult(response);
  }).catchError((e) {
    return _handleError(e);
  });
}

但是(正如其他人所指出的)使用await 而不是Future API 简化了整个事情(尤其是因为我们已经在使用await):

Stream<BlocState> _handleEvent(BlocEvent event) async* {
  try 
    response = await _repository.getData(event.id);
    yield* _processResult(response);
  } catch (e) {
    yield* _handleError(e);
  }
}

有关可运行示例,请参阅 https://dartpad.dartlang.org/fc1ff92e461754bdb35b998e7fbb3406。

【讨论】:

  • 谢谢,当然,_processResult 和 _handleError 是生成器,它们最终将最终状态生成给蒸汽。 yield 在回调中也不起作用,因为它们是闭包。
  • @MotiBartov 我不明白您所说的“yield 在回调中也不起作用,因为它们是闭包”。我是说您需要将(response) { yield* ... } 更改为(response) async* { yield* ... }。
  • 这个我试过了,不行。 (respose){} 实际上是一个内部对象,不是吗?它的范围不允许 yield* 或 yield 到外部类函数的流
  • (response) { ... } 构造一个匿名函数。您绝对可以将async, async*、sync* 添加到匿名函数中。
  • 一般来说你是对的,但请记住,这些外部函数返回 Stream,匿名函数返回类型未定义,所以即使我将它设置为 async* 它也不是同一个流。当我尝试这个时它没有工作。
【解决方案2】:

要处理异步函数中的错误,请使用 try-catch:

try {
  Response response = await _repository.getData(event.id)
} catch (err) {
  print('Caught error: $err');
}

【讨论】:

    【解决方案3】:

    尝试改用try-catch 块。它适用于等待操作。

    【讨论】:

    • 谢谢,我想过这个但是Future api已经有catch error api,我只是想知道我是否可以使用它。这就像用另一个 try-catch 包装 try-catch
    • @MotiBartov 这不是 包装 任何东西。 Dart 编译器会自动将try { await future; } catch { ... } 转换为适当的future.then().catchError() 构造。它是语法糖。
    猜你喜欢
    • 2016-02-18
    • 2022-01-27
    • 2020-08-01
    • 1970-01-01
    • 2022-01-19
    • 2020-01-30
    • 1970-01-01
    • 1970-01-01
    • 2021-04-10
    相关资源
    最近更新 更多