George
George

Reputation: 365

Dart Map.map() using async

I have a Dart Map() which I want to perform a .map() operation on to turn it into a second map. However, this conversion requires an await call. How might I do this in Dart? The below gives me an error of course, but how does one achieve this in one line?

Map secondMap = firstMap.map((key, value) async => MapEntry("key", await someAsyncFunction()))

Upvotes: 1

Views: 704

Answers (1)

Vladimir Gadzhov
Vladimir Gadzhov

Reputation: 824

Here is one way to do it:

final secondMap = Map.fromEntries(await Future.wait(firstMap.entries.map((entry) async => MapEntry(entry.key, await someAsyncFunction()))));

Upvotes: 2

Related Questions