In this recipe, instead of waiting for each task to complete, you will use a FutureGroup to run three asynchronous tasks in parallel:
- Add the following code in the _FuturePageState class, in the main.dart file of your project:
void returnFG() {
FutureGroup<int> futureGroup = FutureGroup<int>();
futureGroup.add(returnOneAsync());
futureGroup.add(returnTwoAsync());
futureGroup.add(returnThreeAsync());
futureGroup.close();
futureGroup.future.then((List <int> value) {
int total = 0;
value.forEach((element) {
total += element;
});
setState(() {
result = total.toString();
});
});
}
- In order to try this code, you can just add the call to returnFG() in the onPressed method of the ElevatedButton (remove or comment out the old code if necessary):
onPressed: () {
returnFG();
}
- Run the code. This time you should see the result faster (after about 3 seconds instead...