In this recipe, you will see the advantages of using the async/await pattern:
- Add the following three methods to the main.dart file, at the bottom of the _FuturePageState class:
Future<int> returnOneAsync() async {
await Future<int>.delayed(const Duration(seconds: 3));
return 1;
}
Future<int> returnTwoAsync() async {
await Future<int>.delayed(const Duration(seconds: 3));
return 2;
}
Future<int> returnThreeAsync() async {
await Future<int>.delayed(const Duration(seconds: 3));
return 3;
}
- Under the three methods you just created, add the count() method leveraging the async/await pattern:
Future count() async {
int total = 0;
total = await returnOneAsync();
total += await returnTwoAsync();
total += await returnThreeAsync();
setState(() {
result = total.toString();
});
}
- Call the count() method from the onPressed function of the "GO" button: