How to avoid using await key in dart Map by foreach function How to avoid using await key in dart Map by foreach function dart dart

How to avoid using await key in dart Map by foreach function


Use a for loop over Map.entries instead of forEach. Provided you are in an async function, await-ing in the body of a for loop will pause the iteration. The entry object will also allow you to access both the key and value.

Future<void> myFunction() async {  for (var entry in myMap.entries) {    await myAsyncFunction(entry.key, entry.value);  }  callFunc();}


You can also use Future.forEach with a map like this :

await Future.forEach(myMap.entries, (MapEntry entry) async {  await myAsyncFunc();          });callFunc();


You could also use map like:

const futures = myMap.map((a, b) => myAsyncFunc());await Future.wait(futures);callFunc();