0
0
Fluttermobile~10 mins

Async/await and Futures in Flutter - Interactive Code Practice

Choose your learning style9 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.