A Future represents a value that will be available later, like waiting for a friend to arrive. It helps handle tasks that take time, such as loading data from the internet.
Click to reveal answer
beginner
What does the async keyword do in a function?
It marks the function as asynchronous, allowing you to use await inside it to pause execution until a Future completes, making code easier to read.
Click to reveal answer
beginner
How does the await keyword work?
It pauses the function execution until the Future finishes and returns its result, like waiting for a text message before continuing.
Click to reveal answer
intermediate
Why use async/await instead of callbacks?
Async/await makes code look simple and linear, avoiding nested callbacks that can be confusing, just like following a clear recipe step-by-step.
Click to reveal answer
intermediate
What happens if a Future throws an error and you use async/await?
You can catch the error using try-catch blocks around the await call, helping you handle problems gracefully without crashing the app.
Click to reveal answer
What type does an async function return in Flutter?
AString
Bvoid
CFuture
Dint
✗ Incorrect
Async functions always return a Future, even if you return a simple value inside.
Which keyword pauses execution until a Future completes?
Aasync
Bthen
Cyield
Dawait
✗ Incorrect
The await keyword pauses the function until the Future finishes.
How do you handle errors in async/await code?
AUsing try-catch blocks
BUsing if-else statements
CUsing switch-case
DErrors cannot be handled
✗ Incorrect
Try-catch blocks let you catch and handle errors from awaited Futures.
What does a Future represent?
AA value available later
BA value available now
CA synchronous function
DA UI widget
✗ Incorrect
A Future represents a value that will be ready in the future.
Which of these is a benefit of async/await?
AMakes code harder to read
BAvoids nested callbacks
CBlocks the UI thread
DRemoves the need for Futures
✗ Incorrect
Async/await helps avoid nested callbacks, making code clearer.
Explain how async and await work together in Flutter to handle long tasks.
Think about waiting for a friend before continuing your plan.
You got /4 concepts.
Describe how you would handle errors when using async/await in Flutter.
Imagine catching a problem before it breaks your app.
You got /4 concepts.
Practice
(1/5)
1.
What does the async keyword do in a Flutter function?
easy
A. It makes the function run faster by using multiple threads.
B. It allows the function to use await to pause until a Future completes.
C. It converts the function into a widget.
D. It automatically retries the function if it fails.
Solution
Step 1: Understand the role of async
The async keyword marks a function so it can pause execution until a Future finishes.
Step 2: Connect async with await
Inside an async function, you can use await to wait for a Future without blocking the app.
Final Answer:
It allows the function to use await to pause until a Future completes. -> Option B
Quick Check:
async enables await usage [OK]
Hint: Remember: async lets you pause with await inside functions [OK]
Common Mistakes:
Thinking async runs code faster by multithreading
Confusing async with UI widgets
Assuming async retries failed calls automatically
2.
Which of the following is the correct way to declare a function that returns a Future of type int?
?
easy
A. Future<int> fetchNumber() async { return 42; }
B. int fetchNumber() async { return 42; }
C. Future fetchNumber() { return 42; }
D. async Future<int> fetchNumber() { return 42; }
Solution
Step 1: Check return type and async keyword
The function must return a Future<int> and be marked async to use await or return a Future.
Step 2: Validate syntax correctness
Future<int> fetchNumber() async { return 42; } correctly declares the return type and uses async before the function body.
Final Answer:
Future<int> fetchNumber() async { return 42; } -> Option A
Quick Check:
Correct Future return type with async [OK]
Hint: Future return type goes before function name, async after [OK]