Dartlang is waiting for more than one future

I want to do something after performing a big future function, boo, I don’t know how to write code in a dart? code like this:

for (var d in data) { d.loadData().then() } // when all loaded // do something here 

but I do not want to wait for them one by one:

 for (var d in data) { await d.loadData(); // NOT NEED THIS } 

How to write this code to the dart?

+12
source share
1 answer

You can use Future.wait to wait for a list of futures:

 import 'dart:async'; Future main() async { var data = []; var futures = <Future>[]; for (var d in data) { futures.add(d.loadData()); } await Future.wait(futures); } 

DartPad Example

+24
source

Source: https://habr.com/ru/post/1014900/


All Articles