0
0
Fluttermobile~20 mins

Async/await and Futures in Flutter - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Async Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
Understanding Future.delayed behavior
What will be displayed on the screen after running this Flutter code snippet?
Flutter
import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({super.key});

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: FutureBuilder<String>(
          future: fetchData(),
          builder: (context, snapshot) {
            if (snapshot.connectionState == ConnectionState.waiting) {
              return const Center(child: Text('Loading...'));
            } else if (snapshot.hasData) {
              return Center(child: Text(snapshot.data!));
            } else {
              return const Center(child: Text('Error'));
            }
          },
        ),
      ),
    );
  }
}
AThe screen shows 'Loading...' for 1 second, then changes to 'Data Loaded'.
BThe screen shows 'Error' because the Future never completes.
CThe screen immediately shows 'Data Loaded' without delay.
DThe screen remains blank because no widget is returned.
Attempts:
2 left
💡 Hint
Think about what Future.delayed does and how FutureBuilder updates UI.
🧠 Conceptual
intermediate
2:00remaining
Identifying the error in async function usage
What error will this Flutter async function cause when called?
Flutter
Future<void> loadData() async {
  String data = fetchData();
  print(data);
}

Future<String> fetchData() async {
  await Future.delayed(const Duration(seconds: 1));
  return 'Hello';
}
ASyntaxError due to missing await keyword.
BNo error; it prints 'Hello' after 1 second.
CTypeError because fetchData() returns a Future<String>, not String.
DRuntime error because fetchData() is not awaited.
Attempts:
2 left
💡 Hint
Check the type returned by fetchData() and how it is assigned.
lifecycle
advanced
2:00remaining
StatefulWidget and async data loading
In this Flutter StatefulWidget, what is the correct way to load async data and update the UI?
Flutter
class MyWidget extends StatefulWidget {
  const MyWidget({super.key});

  @override
  State<MyWidget> createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  String data = 'Loading...';

  @override
  void initState() {
    super.initState();
    fetchData();
  }

  Future<void> fetchData() async {
    final result = await Future.delayed(const Duration(seconds: 1), () => 'Loaded');
    data = result;
  }

  @override
  Widget build(BuildContext context) {
    return Text(data);
  }
}
AThe widget shows 'Loading...' and never updates because setState is missing.
BThe widget shows 'Loading...' and then updates to 'Loaded' automatically.
CThe widget throws an error because fetchData is async in initState.
DThe widget shows 'Loaded' immediately without delay.
Attempts:
2 left
💡 Hint
Think about how Flutter knows to rebuild the UI after data changes.
📝 Syntax
advanced
2:00remaining
Correct async function syntax in Dart
Which option shows the correct syntax for an async function that returns a Future after 2 seconds?
A
Future getNumber() async {
  await Future.delayed(Duration(seconds: 2));
  return '42';
}
B
int getNumber() async {
  await Future.delayed(Duration(seconds: 2));
  return 42;
}
C
Future&lt;int&gt; getNumber() {
  return 42;
}
D
Future&lt;int&gt; getNumber() async {
  await Future.delayed(Duration(seconds: 2));
  return 42;
}
Attempts:
2 left
💡 Hint
Check return types and async keyword usage.
🔧 Debug
expert
2:00remaining
Debugging FutureBuilder with null future
What happens if you pass a null future to FutureBuilder in Flutter?
Flutter
FutureBuilder<String>(
  future: null,
  builder: (context, snapshot) {
    if (snapshot.connectionState == ConnectionState.done) {
      return Text(snapshot.data ?? 'No data');
    } else {
      return const Text('Waiting');
    }
  },
)
AThe builder never runs because future is null.
BThe builder runs immediately with connectionState.done and shows 'No data'.
CThe builder shows 'Waiting' forever because future never completes.
DThe builder throws a runtime error because future is null.
Attempts:
2 left
💡 Hint
Check how FutureBuilder treats a null future.