0
0
Fluttermobile~20 mins

http package in Flutter - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
HTTP Package Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2: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)),
    );
  }
}
AThe UI shows the full HTML content of https://example.com as text.
BThe UI shows the text 'Loading...' forever because fetchData is never called.
CThe UI shows 'Error fetching data' because the URL is invalid.
DThe UI shows a blank screen because setState is not called.
Attempts:
2 left
💡 Hint
Look at when fetchData is called and what happens on a successful response.
📝 Syntax
intermediate
2: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?
Aawait http.post(Uri.parse('https://api.example.com/user'), body: {'name': 'Alice'});
B;))}'ecilA' :'eman'{(edocnEnosj :ydob ,)'resu/moc.elpmaxe.ipa//:sptth'(esrap.irU(tsop.ptth tiawa
Cawait http.post(Uri.parse('https://api.example.com/user'), body: jsonEncode({'name': 'Alice'}));
Dawait http.post(Uri.parse('https://api.example.com/user'), headers: {'Content-Type': 'application/json'}, body: '{"name":"Alice"}');
Attempts:
2 left
💡 Hint
Remember to set the content type header when sending JSON data.
lifecycle
advanced
2: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?
AThe HTTP request runs repeatedly causing performance issues and possible infinite loops.
BThe HTTP request runs once and the UI updates correctly.
CThe HTTP request is ignored by Flutter and does not run.
DThe app crashes with a runtime error.
Attempts:
2 left
💡 Hint
Think about how often build() runs and what happens if you do async calls there.
🔧 Debug
advanced
2: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'));
AUri.parse cannot parse URLs with 'http' scheme.
BThe URL scheme 'htp' is invalid; it should be 'http' or 'https'.
Chttp.get requires a String, not a Uri object.
DThe URL is missing a trailing slash.
Attempts:
2 left
💡 Hint
Check the URL scheme spelling carefully.
🧠 Conceptual
expert
2: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?
AUse http.get without await to avoid exceptions.
BIgnore errors; Flutter will automatically retry failed HTTP requests.
CWrap the http call in try-catch, check response status, and update UI accordingly.
DOnly check response status code; no need for try-catch.
Attempts:
2 left
💡 Hint
Think about network failures and how exceptions are handled in Dart async code.