在 Dart 中是否允许在 for 循环内等待? [英] Await inside for loop is admitted in Dart?

查看:78
本文介绍了在 Dart 中是否允许在 for 循环内等待?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似以下的程序:

I have a program like the following:

main() async {
  ooClass = new OoClass(1);
  int val = await function1();
  print(val);
  ooClass = new OoClass(2);
  val = await function1();
  print(val);
  ooClass = new OoClass(3);
  val = await function1();
  print(val);

}

OoClass ooClass;

Future<int> function1() async {
  List list3 = await function2();
  return list3.indexOf('Ok');
}


Future<List<String>> function2() async {

  List<String> list1 = new List<String>();

  function3(Map<String, int> map1) async {
    String string1 = '';
    bool bool1 = false;
    List<String> list2 = [];
    String string2;

    function4(String string3) async {
      if (ooClass.function7(string3)) return;
      if (ooClass.function8() && !bool1) {
        bool1 = true;
        return;
      }
      string2 = await function5(string3);
      list2.add(string2);
    }

    for (String key in map1.keys) {
      await function4(key);
    }
    string1 = list2.join(', ');
    list1.add(string1);
  }

  for (Map<String, int> idxList in ooClass.function6()) {
    await function3(idxList);
  }
  return list1;
}

function5(String s1) {
  return new Future.value('Ok');
}

class OoClass {

  List<Map<String, int>> map2;
  bool bool3 = false;

  OoClass(int type) {
    switch(type) {
      case 1:
        map2 = [{'Ok':1}];
        break;
      case 2:
        map2 = [{'id': 1, 'Ok':1}];
        break;
      case 3:
        map2 = [{'foo': 1, 'Ok':1}];
        bool3 = true;
        break;
    }
  }

  List<Map<String, int>> function6() {
    return map2;
  }

  bool function7(String string9) {
    if (string9 == 'id') return true;
    return false;
  }

  bool function8() {
    return bool3;
  }
}

此代码段完美运行.

在我的真实环境中,当 await function4(key);被调用时,function2 返回 list1 列表(空).函数4调用稍后执行,但函数2的结果丢失.

In my real environment, instead, when await function4(key); is called, function2 returns the list1 List (empty). Function4 call is executed later but the result of function2 is lost.

我不太理解这种行为.这可能是一个错误或等待内部 for 循环不被使用?如果不应该在 for 循环中使用 await ,我该如何以另一种方式实现它?

I don't really understand this behavior. Could it be a bug or await inside for loop is not to be used? If await should not be used inside for loop how could I implement it in another way?

我使用的是 dart 1.22.0-dev.4,但我也尝试过使用较旧(且稳定)的版本,结果相同.

I'm using dart 1.22.0-dev.4 but I've tried also with older (and stable) versions and I had the same result.

我终于解决了这个问题,它不依赖于 for 循环中的 await.相反,这是我的代码中的一个错误.

I finally got the problem and it did not depend on await in a for loop. It was instead an error in my code.

推荐答案

是的,await 在 Dart 的 for 循环中是允许的,它会按预期工作.

Yes, await is permitted inside a for loop in Dart, and it will work as expected.

for (var o in objects) {
  await doSomething(o);
}

甚至还有 await for 用于 Streams,如果这就是您要寻找的:

And there is even await for for Streams, if that's what you're looking for:

await for (var event in eventStream) {
  print("Event received: $event");
}

您的示例在 DartPad 中正常运行.这太复杂了&抽象来调试,但至少从表面上看,它应该可以工作.不过,您说该代码段在您的真实环境"中不起作用.如果您解释一下您的意思,也许我们可以提供帮助?

Your example works correctly in DartPad. It's too complex & abstract to debug but, at least superficially, it should work. You say that the snippet doesn't work in your "real environment", though. Maybe we could help if you explained what you mean by that?

附加提示:充分利用静态分析,尤其是await_only_futures 和 unawaited_futures linter 规则.这可以帮助您捕获许多错误.

Additional tip: take full advantage of static analysis, especially the await_only_futures and unawaited_futures linter rules. This can help you catch many bugs.

这篇关于在 Dart 中是否允许在 for 循环内等待?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆