在Dart中等待循环进入内部吗? [英] Await inside for loop is admitted in Dart?

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

问题描述

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

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

此代码段效果很好.

在我的真实环境中,当等待function4(key);调用后,function2返回list1 List(空).稍后执行Function4调用,但是function2的结果丢失.

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.

我不太了解这种行为.可能是bug或在内部不使用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.

推荐答案

是的,在Dart的for循环内允许使用await,它将按预期工作.

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

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

如果您正在寻找Streams,甚至还有await for:

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

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中等待循环进入内部吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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