Bird
Raised Fist0
Fluttermobile~10 mins

Async/await and Futures in Flutter - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare an asynchronous function that returns a Future.

Flutter
Future<String> fetchData() async {
  [1] 'Data loaded';
}
Drag options to blanks, or click blank then click option'
Aasync
Breturn
Cyield
Dawait
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'await' instead of 'return' to send back the value.
Trying to use 'yield' which is for streams, not Futures.
2fill in blank
medium

Complete the code to wait for the Future result before printing it.

Flutter
void main() async {
  String data = await fetchData();
  print([1]);
}

Future<String> fetchData() async {
  return 'Hello';
}
Drag options to blanks, or click blank then click option'
Aawait data
BfetchData()
Cdata
Dmain()
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to print the function call instead of the variable.
Using 'await' inside print which is unnecessary here.
3fill in blank
hard

Fix the error in the code by completing the blank to correctly handle the Future.

Flutter
void main() {
  Future<String> future = fetchData();
  future.[1]((value) => print(value));
}

Future<String> fetchData() async {
  return 'Done';
}
Drag options to blanks, or click blank then click option'
AonComplete
Bawait
CcatchError
Dthen
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'await' outside an async function.
Using 'catchError' which handles errors, not success.
4fill in blank
hard

Fill both blanks to create a Future that completes after 2 seconds and returns a message.

Flutter
Future<String> delayedMessage() {
  return Future.[1](Duration(seconds: 2), () => [2]);
}
Drag options to blanks, or click blank then click option'
Adelayed
B'Hello after delay'
Cprint('Hello')
Dimmediate
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'immediate' which is not a Future method.
Trying to print inside the Future instead of returning a string.
5fill in blank
hard

Fill all three blanks to create an async function that waits for two Futures and returns their combined result.

Flutter
Future<String> combinedData() async {
  String first = await fetchFirst();
  String second = await [1]();
  return first + [2] + [3];
}

Future<String> fetchFirst() async => 'Hello';
Future<String> fetchSecond() async => 'World';
Drag options to blanks, or click blank then click option'
AfetchSecond
B' '
Csecond
DfetchFirst
Attempts:
3 left
💡 Hint
Common Mistakes
Calling fetchFirst() again instead of fetchSecond().
Forgetting to add a space between the words.
Returning variables that are not defined.

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

  1. Step 1: Understand the role of async

    The async keyword marks a function so it can pause execution until a Future finishes.
  2. Step 2: Connect async with await

    Inside an async function, you can use await to wait for a Future without blocking the app.
  3. Final Answer:

    It allows the function to use await to pause until a Future completes. -> Option B
  4. 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

  1. 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.
  2. Step 2: Validate syntax correctness

    Future<int> fetchNumber() async { return 42; } correctly declares the return type and uses async before the function body.
  3. Final Answer:

    Future<int> fetchNumber() async { return 42; } -> Option A
  4. Quick Check:

    Correct Future return type with async [OK]
Hint: Future return type goes before function name, async after [OK]
Common Mistakes:
  • Placing async before Future in declaration
  • Missing Future return type
  • Returning int directly without Future
3.

What will be printed by this Flutter code?

Future<String> fetchData() async {
  await Future.delayed(Duration(seconds: 1));
  return 'Done';
}

void main() async {
  print('Start');
  var result = await fetchData();
  print(result);
  print('End');
}
medium
A. Start\nDone\nEnd
B. Done\nStart\nEnd
C. Start\nEnd\nDone
D. Start\nDone

Solution

  1. Step 1: Trace the print statements and await

    First, 'Start' prints immediately. Then await fetchData() pauses main until fetchData completes after 1 second.
  2. Step 2: After fetchData returns, print result and 'End'

    Once fetchData returns 'Done', it prints 'Done' then prints 'End'.
  3. Final Answer:

    Start\nDone\nEnd -> Option A
  4. Quick Check:

    Await pauses main, so prints in order [OK]
Hint: Await pauses code, so prints happen in sequence [OK]
Common Mistakes:
  • Assuming 'Done' prints before 'Start'
  • Ignoring await delay effect
  • Thinking 'End' prints before 'Done'
4.

Identify the error in this Flutter async code snippet:

Future<void> loadData() async {
  var data = fetchData();
  print(data);
}

Future<String> fetchData() async {
  await Future.delayed(Duration(seconds: 1));
  return 'Data loaded';
}
medium
A. print cannot be used inside async functions.
B. fetchData() should not be async.
C. Missing await before fetchData() causing print to show Future instance.
D. Duration.seconds is invalid syntax.

Solution

  1. Step 1: Check how fetchData() is called

    fetchData() returns a Future<String>, but loadData() does not await it, so data is a Future, not the string.
  2. Step 2: Understand print output

    Printing data prints the Future object, not the resolved string. Adding await fixes this.
  3. Final Answer:

    Missing await before fetchData() causing print to show Future instance. -> Option C
  4. Quick Check:

    Await needed to get Future result [OK]
Hint: Always await Futures before using their results [OK]
Common Mistakes:
  • Forgetting to await async calls
  • Thinking print can't be used in async
  • Misreading Duration syntax
5.

You want to fetch user data and then fetch user posts only after the user data is ready. Which code correctly uses async/await to do this?

Future<String> fetchUser() async {
  await Future.delayed(Duration(seconds: 1));
  return 'User123';
}

Future fetchPosts(String user) async {
  await Future.delayed(Duration(seconds: 1));
  return ['Post1', 'Post2'];
}

void main() async {
  // Fill in the blanks
  var user = ?;
  var posts = ?;
  print('User: $user');
  print('Posts: $posts');
}
hard
A. var user = fetchUser();\nvar posts = fetchPosts(user);
B. var user = await fetchUser();\nvar posts = fetchPosts(user);
C. var user = fetchUser();\nvar posts = await fetchPosts(await user);
D. var user = await fetchUser();\nvar posts = await fetchPosts(user);

Solution

  1. Step 1: Await fetchUser() to get user before fetching posts

    We must wait for fetchUser() to complete to get the user string before calling fetchPosts.
  2. Step 2: Await fetchPosts(user) to get posts list

    Since fetchPosts returns a Future, we await it to get the actual list before printing.
  3. Final Answer:

    var user = await fetchUser();\nvar posts = await fetchPosts(user); -> Option D
  4. Quick Check:

    Await both calls in sequence for correct data [OK]
Hint: Await each async call in order to get real results [OK]
Common Mistakes:
  • Not awaiting fetchUser before fetchPosts
  • Not awaiting fetchPosts result
  • Trying to await non-Future values
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard