On a slow connection in Damongo, loading a list of prices from a server might take several seconds. If your app simply stopped and waited, the screen would freeze, buttons would stop responding and users would think it had crashed. Asynchronous programming lets your app start slow work and keep the screen responsive while it waits. Every app that talks to a server or a database depends on it.
What a Future is
A Future is a promise of a value that will arrive later. When you ask for data from the internet, you get back a Future immediately, and the real data arrives when the request finishes.
Future<String> fetchGreeting() {
return Future.delayed(
Duration(seconds: 2),
() => 'Good morning, Amina',
);
}Future.delayed pretends to be slow work. In a real app this would be a network request or a database read.
async and await
The keywords async and await let you write code that waits for a Future but still reads from top to bottom:
Future<void> main() async {
print('Loading...');
var message = await fetchGreeting();
print(message);
print('Done');
}