If you want to wait for multiple futures of different types and also support null-safety then you can add a helper function similar to the following when using Dart >= 3.0:
Future<(T1, T2)> waitConcurrently<T1, T2>(
Future<T1> future1,
Future<T2> future2,
) async {
late T1 result1;
late T2 result2;
await Future.wait([
future1.then((value) => result1 = value),
future2.then((value) => result2 = value)
]);
return (result1, result2);
}
Dart < 3.0 does not support tuples natively, but there is a package from Google which does: https://pub.dev/packages/tuple
import 'package:tuple/tuple.dart';
Future<Tuple2<T1, T2>> waitConcurrently<T1, T2>(
Future<T1> future1, Future<T2> future2) async {
late T1 result1;
late T2 result2;
await Future.wait([
future1.then((value) => result1 = value),
future2.then((value) => result2 = value)
]);
return Tuple2(result1, result2);
}
map
as well:await Future.wait(data.map((d)=>d.loadData()))
. – Niggerhead