Challenge - 5 Problems
HTTP Package Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What is the output of this Flutter code using http package?
Consider this Flutter code snippet that fetches data from a URL and displays the response body in a Text widget. What will the UI show after a successful fetch?
Flutter
import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; class MyWidget extends StatefulWidget { @override State<MyWidget> createState() => _MyWidgetState(); } class _MyWidgetState extends State<MyWidget> { String data = 'Loading...'; @override void initState() { super.initState(); fetchData(); } Future<void> fetchData() async { final response = await http.get(Uri.parse('https://example.com')); if (response.statusCode == 200) { setState(() { data = response.body; }); } else { setState(() { data = 'Error fetching data'; }); } } @override Widget build(BuildContext context) { return Scaffold( body: Center(child: Text(data)), ); } }
Attempts:
2 left
💡 Hint
Look at when fetchData is called and what happens on a successful response.
✗ Incorrect
The fetchData function is called in initState, so the HTTP GET request runs once the widget loads. If the response status is 200, the response body is saved in the state variable 'data' and displayed in the Text widget. So the UI will show the full content fetched from the URL.
📝 Syntax
intermediate2:00remaining
Which option correctly creates a POST request with JSON body using http package?
You want to send a POST request with JSON data {'name': 'Alice'} to 'https://api.example.com/user'. Which code snippet is correct?
Attempts:
2 left
💡 Hint
Remember to set the content type header when sending JSON data.
✗ Incorrect
Option D correctly sets the 'Content-Type' header to 'application/json' and sends the JSON string as the body. Option D sends a Map without encoding, which is invalid. Option D misses headers. Option D misses headers, so the server may not recognize the body as JSON.
❓ lifecycle
advanced2:00remaining
What happens if you call http.get inside build() method directly?
In a Flutter widget, what is the effect of calling http.get inside the build() method without caching or state management?
Attempts:
2 left
💡 Hint
Think about how often build() runs and what happens if you do async calls there.
✗ Incorrect
The build() method can be called many times during widget lifecycle. Calling http.get inside build() causes repeated network requests, leading to performance problems and possibly infinite loops if setState is called after each fetch.
🔧 Debug
advanced2:00remaining
Why does this http.get code throw a FormatException?
This code throws a FormatException: Invalid argument(s): Invalid URL. What is the cause?
Flutter
final response = await http.get(Uri.parse('htp://invalid-url'));
Attempts:
2 left
💡 Hint
Check the URL scheme spelling carefully.
✗ Incorrect
The URL scheme 'htp' is misspelled. Uri.parse throws FormatException because it expects 'http' or 'https' schemes.
🧠 Conceptual
expert2:00remaining
Which option best describes how to handle HTTP errors gracefully in Flutter using http package?
You want your app to show a friendly message if the HTTP request fails due to network issues or server errors. Which approach is best?
Attempts:
2 left
💡 Hint
Think about network failures and how exceptions are handled in Dart async code.
✗ Incorrect
Network calls can fail with exceptions or return error status codes. Wrapping calls in try-catch and checking status codes lets you handle errors gracefully and update the UI with friendly messages.