'Getting null in response with get API

Hello I have this package http package

Have it, in the file pubspec.yaml on dependencies http: ^0.12.0+2 . I get the packages.

Import

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

Code

http.Response response = await http.get(
    'https://api.openweathermap.org/data/2.5/weather?lat=$latitude&lon=$longitude&appid=$API_KEY'
);

I debug it and the value of response is always null. So I never know the status code of the response. I also try it with other APIs but I have the same problem. I search the URL on the website and it gives me a JSON, that is what I'm looking for.

Any suggestion? Debug image

I also try an example and it fails.

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

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

  // Await the http get response, then decode the json-formatted responce.
  var response = await http.get(url);
  if (response.statusCode == 200) {
    var jsonResponse = convert.jsonDecode(response.body);
    var itemCount = jsonResponse['totalItems'];
    print("Number of books about http: $itemCount.");
  } else {
    print("Request failed with status: ${response.statusCode}.");
  }
}


Solution 1:[1]

The reason for my problem was the Android manifest, I'm new and I didn't know what that it can't have newlines in blank, so I join it and it works. I'm crying XD ?

Solution 2:[2]

This Code works for me:

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

class StackOverflow extends StatefulWidget {
  @override
  _StackOverflowState createState() => _StackOverflowState();
}

class _StackOverflowState extends State<StackOverflow> {
  @override
  void initState() {
    super.initState();
    _getHttp().then((response){
      print('response code = '+ response.statusCode.toString());
    });

  }

  Future<http.Response> _getHttp() async {
    String latitude = 'latitude';
    String longitude = 'longitude';
    String aPI_KEY = 'API_KEY';

    try {
      http.Response response = await http.get(
        'https://api.openweathermap.org/data/2.5/weather?lat=$latitude&lon=$longitude&appid=$aPI_KEY');
        return response;
    } catch (e) {
      print('Exception = ' + e.toString());
      throw Exception(e);
    }


  }

  @override
  Widget build(BuildContext context) {
    return Scaffold();
  }
}

Solution 3:[3]

update your manifest add this uses library inside Application {Image 1}

   <uses-library
    android:name="org.apache.http.legacy"
    android:required="false" />

and also make a network security config xml file and add this inside res folder {Image 2}

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true">
        <trust-anchors>
            <certificates src="system" />
            <certificates src="user" />
        </trust-anchors>
    </base-config>
<!--    <domain-config cleartextTrafficPermitted="false">-->
<!--&lt;!&ndash;        <domain includeSubdomains="true">secure.example.com</domain>&ndash;&gt;-->
<!--    </domain-config>-->
</network-security-config>

and update your manifest by adding metadata {Image 3}

<meta-data android:name="io.flutter.network-policy"
    android:resource="@xml/network_security_config"/>

Add library

Add xml file

Update manifest like this

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jonathan Gómez
Solution 2
Solution 3 Rasel Khan