Await inside for loop is admitted in Dart?

后端 未结 1 1890
悲哀的现实
悲哀的现实 2020-12-10 11:43

I have a program like the following:

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


        
相关标签:
1条回答
  • 2020-12-10 11:56

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

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

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

    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?

    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.

    0 讨论(0)
提交回复
热议问题