Challenge - 5 Problems
Network Error Handling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
Handling network error UI feedback
You have a Flutter app that fetches data from the internet. Which option correctly shows a simple error message on the screen when the network call fails?
Flutter
Future<String> fetchData() async { throw Exception('Network error'); } class MyWidget extends StatefulWidget { @override State<MyWidget> createState() => _MyWidgetState(); } class _MyWidgetState extends State<MyWidget> { late Future<String> data; @override void initState() { super.initState(); data = fetchData(); } @override Widget build(BuildContext context) { return FutureBuilder<String>( future: data, builder: (context, snapshot) { if (snapshot.hasError) { return Text('Error occurred'); } else if (snapshot.hasData) { return Text(snapshot.data!); } else { return CircularProgressIndicator(); } }, ); } }
Attempts:
2 left
💡 Hint
FutureBuilder has a property to detect errors from the future.
✗ Incorrect
FutureBuilder's snapshot.hasError is the correct way to detect and show errors from async calls in Flutter UI.
🧠 Conceptual
intermediate1:30remaining
Understanding try-catch with async network calls
What happens if you do not use try-catch when awaiting a network call that throws an exception in Flutter?
Flutter
Future<String> fetchData() async { throw Exception('Network failure'); } void load() async { var result = await fetchData(); print(result); }
Attempts:
2 left
💡 Hint
Think about what happens when an exception is not caught in async code.
✗ Incorrect
If an exception is not caught, it propagates and can crash the app unless handled higher up.
❓ lifecycle
advanced2:30remaining
Properly cancelling network calls on widget disposal
You start a network call in initState() of a StatefulWidget. What is the best way to avoid updating the UI after the widget is disposed?
Flutter
class MyWidget extends StatefulWidget { @override State<MyWidget> createState() => _MyWidgetState(); } class _MyWidgetState extends State<MyWidget> { late Future<String> data; @override void initState() { super.initState(); data = fetchData(); } @override Widget build(BuildContext context) { return FutureBuilder<String>(future: data, builder: (context, snapshot) { if (snapshot.hasData) return Text(snapshot.data!); return CircularProgressIndicator(); }); } @override void dispose() { super.dispose(); } }
Attempts:
2 left
💡 Hint
Widgets can be disposed before async calls finish, causing errors if UI updates happen.
✗ Incorrect
Checking if the widget is still mounted before calling setState or using cancellable futures prevents errors after disposal.
📝 Syntax
advanced2:00remaining
Identify the error in this network call error handling code
What error does this Flutter code produce when fetchData throws an exception?
Flutter
Future<String> fetchData() async { throw Exception('Failed'); } void loadData() async { try { var data = fetchData(); print(data); } catch (e) { print('Error caught'); } }
Attempts:
2 left
💡 Hint
Look carefully at how fetchData() is called inside try block.
✗ Incorrect
Without await, fetchData() returns a Future immediately, so print prints the Future object, not the data or error.
🔧 Debug
expert3:00remaining
Debugging silent failure in network call error handling
A Flutter app uses this code to fetch data and show errors. But when the network fails, no error message appears and the loading spinner stays forever. What is the cause?
Flutter
Future<String> fetchData() async { throw Exception('Network down'); } class MyWidget extends StatefulWidget { @override State<MyWidget> createState() => _MyWidgetState(); } class _MyWidgetState extends State<MyWidget> { late Future<String> data; @override void initState() { super.initState(); data = fetchData().catchError((e) { print('Caught error'); }); } @override Widget build(BuildContext context) { return FutureBuilder<String>( future: data, builder: (context, snapshot) { if (snapshot.hasError) { return Text('Error occurred'); } else if (snapshot.hasData) { return Text(snapshot.data!); } else { return CircularProgressIndicator(); } }, ); } }
Attempts:
2 left
💡 Hint
catchError can transform the Future result and affect error propagation.
✗ Incorrect
Using catchError without rethrowing converts the Future to a successful one, so FutureBuilder never sees an error and shows loading forever.