Async: Think of it as saying, "I'm going to do something that takes time, but I won't stop everything else." So, you're telling Dart, "While the bread is toasting, I can do other tasks."
Await: It's like pausing to check if something is ready. In our sandwich example, it's like saying, "Wait until the bread is done toasting before moving on."
Future: This is Dart's way of promising something. It's like telling Dart, "I'm working on getting something for you, and when it's ready, I'll let you know."
Let's Understand with one Example :
// Async function that returns a Future
Future<String> makeSandwich() async {
print("Putting bread in toaster...");
// Simulating toasting time
await Future.delayed(Duration(seconds: 3));
print("Toasted bread is ready!");
return "Delicious sandwich";
}
void main() async {
print("Starting sandwich making process...");
// Await the Future to get the result
String sandwich = await makeSandwich();
print("Made a sandwich: $sandwich");
print("Lunchtime is a success!");
}
Output:
ย