getting weather info using flutter http package

307 views Asked by At

i want to get current weather information of coordinated base from openweathermap API. For this purpose i am using flutter's HTTP Package.

the problem is here when i use this method

One more problem is that... I got these coordinates on Memu App Player but does not gives on Real android device and Android Studio's Own Emulator.

void getWeatherData()async{
String url = 'http://api.openweathermap.org/data/2.5/weather?lat=35&lon=139&appid=331533c0cc2197e929ea79cdb2a70e33';
Response response = await get(Uri.parse(url));
print(response.body);  }

this should give me API result completely but it gives me this error when i run my app.

Unhandled Exception: Bad state: Insecure HTTP is not allowed by platform: http://api.openweathermap.org/data/2.5/weather?lat=35&lon=139&appid=331533c0cc2197e929ea79cdb2a70e33
1

There are 1 answers

0
GreddyDC5 On

This is how I did it. I'm going through a course in Udemy that is going over what you are asking.

void getData() async {

    var url = Uri.https('samples.openweathermap.org', 'data/2.5/weather', {
      'lat': '35',
      'lon': '139',
      'appid': 'b6907d289e10d714a6e88b30761fae22'
    });

    http.Response response = await http.get(url);

    print(weatherResponse.body);
}

Remember to use

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