0
0
Fluttermobile~20 mins

GET and POST requests in Flutter - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
HTTP Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2: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)),
    );
  }
}
AThe screen shows the text content fetched from https://example.com/data.
BThe screen shows the text 'Loading...' indefinitely.
CThe screen shows a blank white screen with no text.
DThe app crashes with a runtime error due to missing internet permission.
Attempts:
2 left
💡 Hint
Think about what happens after the HTTP GET request completes successfully.
📝 Syntax
intermediate
2: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?
Aawait http.post(Uri.parse('https://example.com/api'), body: jsonEncode({'name': 'Alice'}), headers: {'Content-Type': 'text/plain'});
Bawait http.post(Uri.parse('https://example.com/api'), body: {'name': 'Alice'});
Cawait http.post('https://example.com/api', body: jsonEncode({'name': 'Alice'}));
Dawait http.post(Uri.parse('https://example.com/api'), headers: {'Content-Type': 'application/json'}, body: jsonEncode({'name': 'Alice'}));
Attempts:
2 left
💡 Hint
Remember to set the content type header to 'application/json' and encode the body as JSON string.
lifecycle
advanced
2: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...');
  }
}
AThe GET request runs only once and works fine.
BThe GET request runs repeatedly causing performance issues and multiple prints.
CThe code causes a compile-time error because http.get is not allowed in build.
DThe app crashes because build method cannot have async calls.
Attempts:
2 left
💡 Hint
Think about how often build() runs in Flutter widgets.
🔧 Debug
advanced
2: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'}),
);
AThe Content-Type header is missing or incorrect.
BThe URL is invalid and causes server error.
CThe body is not encoded as JSON string.
DThe http.post method requires a GET request instead.
Attempts:
2 left
💡 Hint
Check what header tells the server the data format.
🧠 Conceptual
expert
2: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?
AUse only .then() without error handling callbacks.
BIgnore errors and assume the request always succeeds.
CUse try-catch around await http.get and check response status code inside try block.
DCall http.get without await and do not check response.
Attempts:
2 left
💡 Hint
Think about catching exceptions and validating responses.