Challenge - 5 Problems
Async Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2: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')); } }, ), ), ); } }
Attempts:
2 left
💡 Hint
Think about what Future.delayed does and how FutureBuilder updates UI.
✗ Incorrect
The FutureBuilder shows 'Loading...' while waiting for the Future to complete. After 1 second delay, fetchData returns 'Data Loaded', which is then displayed.
🧠 Conceptual
intermediate2: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'; }
Attempts:
2 left
💡 Hint
Check the type returned by fetchData() and how it is assigned.
✗ Incorrect
fetchData() returns a Future, but the code assigns it directly to a String variable without awaiting, causing a type mismatch error.
❓ lifecycle
advanced2: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); } }
Attempts:
2 left
💡 Hint
Think about how Flutter knows to rebuild the UI after data changes.
✗ Incorrect
Without calling setState after data changes, Flutter does not rebuild the widget, so the UI stays showing 'Loading...'.
📝 Syntax
advanced2:00remaining
Correct async function syntax in Dart
Which option shows the correct syntax for an async function that returns a Future after 2 seconds?
Attempts:
2 left
💡 Hint
Check return types and async keyword usage.
✗ Incorrect
Option D correctly declares a Future return type, uses async, awaits delay, and returns an int. Option D has wrong return type. Option D is not async and returns int directly. Option D returns a String instead of int.
🔧 Debug
expert2: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'); } }, )
Attempts:
2 left
💡 Hint
Check how FutureBuilder treats a null future.
✗ Incorrect
If future is null, FutureBuilder treats it as already completed with no data, so connectionState is done and snapshot.data is null.