Challenge - 5 Problems
HTTP Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What happens when this Flutter code runs?
This Flutter widget fetches data from a URL using a GET request and displays the result. What will the UI show after the data is successfully fetched?
Flutter
import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; class DataFetcher extends StatefulWidget { @override State<DataFetcher> createState() => _DataFetcherState(); } class _DataFetcherState extends State<DataFetcher> { String data = 'Loading...'; @override void initState() { super.initState(); fetchData(); } Future<void> fetchData() async { final response = await http.get(Uri.parse('https://example.com/data')); 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
Think about what happens after the HTTP GET request completes successfully.
✗ Incorrect
The widget starts with 'Loading...'. After the GET request completes with status 200, it updates the state to show the response body text on screen.
📝 Syntax
intermediate2:00remaining
Which option correctly sends a POST request with JSON body in Flutter?
You want to send a POST request with JSON data {'name': 'Alice'} to https://example.com/api. Which code snippet is correct?
Attempts:
2 left
💡 Hint
Remember to set the content type header to 'application/json' and encode the body as JSON string.
✗ Incorrect
Option D correctly sets the header and encodes the body as JSON string. Option D sends a Map as body without encoding. Option D misses Uri.parse. Option D sets wrong content type.
❓ lifecycle
advanced2:00remaining
What is the problem with this Flutter GET request code inside build()?
Consider this Flutter widget code snippet that fetches data inside the build method. What issue will it cause?
Flutter
import 'package:http/http.dart' as http; class MyWidget extends StatelessWidget { @override Widget build(BuildContext context) { http.get(Uri.parse('https://example.com/data')).then((response) { print(response.body); }); return Text('Fetching data...'); } }
Attempts:
2 left
💡 Hint
Think about how often build() runs in Flutter widgets.
✗ Incorrect
The build method can be called many times, so placing http.get inside it causes repeated network calls and prints, hurting performance.
🔧 Debug
advanced2:00remaining
Why does this POST request fail with a 415 Unsupported Media Type error?
This Flutter code sends a POST request but the server responds with 415 error. What is the cause?
Flutter
final response = await http.post( Uri.parse('https://example.com/api'), body: jsonEncode({'key': 'value'}), );
Attempts:
2 left
💡 Hint
Check what header tells the server the data format.
✗ Incorrect
Without setting 'Content-Type' to 'application/json', the server rejects the JSON body causing 415 error.
🧠 Conceptual
expert2:00remaining
What is the best way to handle errors in Flutter async HTTP requests?
You want to fetch data with GET and handle possible errors like network failure or bad response. Which approach is best?
Attempts:
2 left
💡 Hint
Think about catching exceptions and validating responses.
✗ Incorrect
Using try-catch with await allows catching exceptions like network errors, and checking status code ensures response is valid.