To execute all futures concurrently, use Future.wait
This takes a list of futures and returns a future of lists: Suppose you have these futures.
要同时执行所有future,请使用 Future.wait
这将获取一个future列表并返回一个future列表:假设你有这些未来。
class CovidAPI { Future<int> getCases() => Future.value(1000); Future<int> getRecovered() => Future.value(100); Future<int> getDeaths() => Future.value(10); }
You can get all the futures together using Future.wait([list of futures])
您可以使用Future. wait([list of futures])将所有的期货集合在一起
final api = CovidAPI(); final values = await Future.wait([ api.getCases(), api.getRecovered(), api.getDeaths(), ]); print(values); // [1000, 100, 10]
This is ideal when the futures are independent, and they don't need to execute sequentially. Source : https://codewithandrea.com/videos/top-dart-tips-and-tricks-for-flutter-devs/
当future是独立的,并且它们不需要顺序执行时,这是理想的。来源: https://codewithandrea.com/videos/top-dart-tips-and-tricks-for-flutter-devs/