In programming, sometimes tasks take time to complete — like loading data from the internet, reading files, or waiting for a timer. Asynchronous programming helps your app stay responsive by allowing these tasks to run in the background without freezing the app.
// Function that simulates waiting for 3 seconds before returning a message FuturefetchData() async { await Future.delayed(Duration(seconds: 3)); // wait 3 seconds return 'Data loaded!'; } void main() async { print('Fetching data...'); // Wait for fetchData to complete, but without freezing main String result = await fetchData(); print(result); }
Future<String> means this function returns a value later (a String).async marks a function to use await inside it.await pauses code execution until the Future completes, but only inside async functions.Future.delayed() simulates a delay (like waiting for data).You can use Futures with .then() callbacks, but async/await makes the code look cleaner and easier to read, especially when chaining many asynchronous operations.
fetchData().then((result) {
print(result);
});
This works fine, but for multiple steps, callbacks can get messy — async/await simplifies that.
Imagine ordering a pizza. Instead of standing in the kitchen waiting for the pizza (blocking everything), you place your order and do other things. Later, when the pizza arrives (Future completes), you get notified and enjoy your pizza. This way, you don’t waste time waiting.
For a deeper dive, check out the official Flutter docs on Async & Await.