0
0
Fluttermobile~10 mins

GET and POST requests in Flutter - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the package needed for HTTP requests in Flutter.

Flutter
import [1];
Drag options to blanks, or click blank then click option'
Apackage:flutter/material.dart
Bpackage:flutter/widgets.dart
C'package:http/http.dart' as http
Dpackage:async/async.dart
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Flutter UI packages instead of the HTTP package.
Forgetting to alias the package as 'http' for easier use.
2fill in blank
medium

Complete the code to send a GET request to the given URL.

Flutter
final response = await http.[1](Uri.parse('https://example.com/data'));
Drag options to blanks, or click blank then click option'
Adelete
Bpost
Cput
Dget
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'post' instead of 'get' for fetching data.
Not wrapping the URL string with Uri.parse().
3fill in blank
hard

Fix the error in the POST request code by completing the missing method.

Flutter
final response = await http.[1](Uri.parse('https://example.com/post'), body: {'key': 'value'});
Drag options to blanks, or click blank then click option'
Apost
Bget
Cpatch
Dhead
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'get' method with a body parameter, which is invalid.
Confusing HTTP methods and their purposes.
4fill in blank
hard

Fill both blanks to check if the GET request was successful and print the response body.

Flutter
if (response.statusCode [1] 200) {
  print(response.[2]);
}
Drag options to blanks, or click blank then click option'
A==
BstatusCode
Cbody
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '==' to check status code.
Printing 'statusCode' instead of 'body'.
5fill in blank
hard

Fill all three blanks to send a POST request with JSON headers and body.

Flutter
final response = await http.[1](
  Uri.parse('https://example.com/api'),
  headers: {'Content-Type': '[2]'},
  body: [3],
);
Drag options to blanks, or click blank then click option'
Apost
Bapplication/json
CjsonEncode({'name': 'John'})
Dget
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'get' instead of 'post' for sending data.
Forgetting to set 'Content-Type' header.
Sending body as a Dart map instead of JSON string.