Async and Practical
Future.wait
Combine Several Futures
Future.wait(futures) returns one Future that completes with a list once every input future is ready. Each score here is an async function that returns immediately, so the whole example settles without timers or external events.
Program
Play the program to await three scores together and combine them.
future_wait.dart
Future<int> score(String name) async {
if (name == 'Ada') return 9;
if (name == 'Lin') return 12;
return 6;
}
Future<void> main() async {
var futures = [score('Ada'), score('Lin'), score('Mia')];
var results = await Future.wait(futures);
var total = results.reduce((a, b) => a + b);
print('$results sum=$total');
}
Future.wait
`Future.wait(futures)` produces one future that completes with a list of all results in order.
already-completed
Each `async` `return` completes that function's future with a value, so no timers, files, or network are involved.
aggregate
`results.reduce((a, b) => a + b)` collapses the list into one total once the futures have all settled.