Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

flutter
http
get
communitycreator

What is an HTTP GET response in Flutter?

Irzum Jafri

Overview

Flutter is great when developing applications, but an important component of developing these applications is connecting them to the backend service.

HTTP is a powerful protocol used to connect applications and the server. The most commonly used service for retrieving data online is an HTTP GET request. The protocol is stateless, and therefore, it can easily manipulate data on the server directly, even when handling multiple devices.

Flutter makes it simple to handle these requests with its HTTP package. To use this package in your application, you can run "flutter pub add http" in your terminal or add the following line in your pubspec.yaml file.

dependencies:
  http: ^0.13.4

If you are creating an android application, you will need authorization, for which permission to use the internet will be required from the application. To do this, add the following line in your AndroidManifest.xml file.

<uses-permission android:name="android.permission.INTERNET" />

After adding the line above, run "flutter pub get" to update your application files and install the package in your application.

Once the dependency is added, you will import the package into your Dart file and call the package function to generate your response.

void main(List<String> arguments) async {
  var url = Uri.https('www.googleapis.com', '/books/v1/volumes', {'q': '{http}'});
  var response = await http.get(url);
}

The aforementioned function calls the HTTP GET response and waits for the server to get the response. After that, this data, received as a JSON string, can be manipulated according to your needs.

You can find the complete application code below.

import 'dart:convert' as convert;
import 'package:http/http.dart' as http;

void main(List<String> arguments) async {
  var url = Uri.https('www.googleapis.com', '/books/v1/volumes', {'q': '{http}'});

  // You will wait for the response and then decode the JSON string.
  var response = await http.get(url);
  if (response.statusCode != 200) {
    print("HTTP GET RESPONSE REQUEST FAILED.")
  } else {
    var jsonResponse =
        convert.jsonDecode(response.body) as Map<String, dynamic>;
    var itemCount = jsonResponse['totalItems'];
    print('Number of books about http: $itemCount.');
  }
}

RELATED TAGS

flutter
http
get
communitycreator
RELATED COURSES

View all Courses

Keep Exploring