このページはまだ翻訳されていないため、英語で表示されます。 翻訳に参加する

resolveProps

Awaits every value of a Map and returns the resolved Map — the map-shaped analogue of Future.wait.

Future<Map<K, V>> resolveProps<K, V>(Map<K, FutureOr<V>> map)

Lecture

resolveProps solves a specific, common shape of problem: you have a Map whose values are a mix of plain values and Futures — say, several independent API calls keyed by field name — and you want one Future back for the whole, fully-resolved map.

Internally it loops over the entries and awaits each value in turn, which reads like it should be sequential. In practice, it usually isn't slower than doing them all "at once": a Dart Future starts running the moment it's created, not when it's awaited. So if you build the map with delay(...) or already-in-flight requests as its values — the normal way to call this function — every one of them is already racing by the time resolveProps gets the map; its sequential await loop is just reading off results that are largely ready already, not gating when the work starts. The demo below proves this with a stopwatch.

Demo 1 · Basics

Demo 2 · The futures already overlap

Three 100ms delays, but the whole thing finishes well under 300ms — because they all started together:

Try it yourself

Exercise: use resolveProps to await every value in requests.

Related: evolve — the sync cousin, transforming instead of awaiting · concurrent — the iterable-shaped version of "run things together" · delay & sleep — used to build the demo futures · fromEntries — build a Map from scratch