Async and Practical
async/await
Pause and Resume
async marks a function that returns a Future; await pauses the surrounding async function until the future is ready, then resumes with the value. Future.value(x) produces an already-completed future, so the example is deterministic and free of timing.
Program
Play the program to await two futures in sequence and watch each resume bind its result.
async_await.dart
Future<int> getCount() {
return Future.value(7);
}
Future<int> twice(int n) async {
return n * 2;
}
Future<void> main() async {
var a = await getCount();
var b = await twice(a);
print('$a $b');
}
async function
An `async` function (like `twice`) returns a `Future<T>` automatically; `return n * 2` becomes `Future.value(n * 2)`.
Future.value
`Future.value(7)` is an already-completed future, so the example needs no timers, files, network, or external events.
pause and resume
Each `await fn()` suspends `main` until the future is ready, then resumes with the value bound into a local.