Complete the code to declare an asynchronous function that returns a Future.
Future<String> fetchData() async {
[1] 'Data loaded';
}The return keyword is used to send back the result from the async function wrapped in a Future.
Complete the code to wait for the Future result before printing it.
void main() async {
String data = await fetchData();
print([1]);
}
Future<String> fetchData() async {
return 'Hello';
}You print the variable data which holds the awaited result.
Fix the error in the code by completing the blank to correctly handle the Future.
void main() {
Future<String> future = fetchData();
future.[1]((value) => print(value));
}
Future<String> fetchData() async {
return 'Done';
}The then method is used to register a callback when the Future completes successfully.
Fill both blanks to create a Future that completes after 2 seconds and returns a message.
Future<String> delayedMessage() {
return Future.[1](Duration(seconds: 2), () => [2]);
}Future.delayed creates a Future that completes after the given duration. The second argument is the value returned when it completes.
Fill all three blanks to create an async function that waits for two Futures and returns their combined result.
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';The function waits for fetchSecond(), then returns the concatenation of first, a space, and second.