Search code examples
androidflutterdartresponse

open weather map not response on flutter


there is some issues from res and site with this error :

" I/flutter ( 1528): SocketException: Failed host lookup: 'samples.openweathermap.org' (OS Error: No address associated with hostname, errno = 7) "

I try several ways to solve the problem like :

  1. turn on gps
  2. turn on internet of emu or real device
  3. add uses-permission android:name="android.permission.INTERNET" to manifest
  4. restart the emu and real device

but all time flutter send NULL or Error exp latitude and longitude also are Null but in emu explained T for that , hardcode them...

code :

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

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    // TODO: get currentLocation
    super.initState();
    getLocation();
  }

  void getLocation() async {
    Locate location = new Locate();
    await location.getLocate();
    if (location.latitude == null && location.longitude == null) {
      print('------------- Error - null Fuck exp!! -------------');
    }
    print('latitude : ${location.latitude}');
    print('longitude : ${location.longitude}');
    getApi(location);
  }

  void getApi(Locate location) async {
    try {
      http.Response response = await http.get(
          'https://samples.openweathermap.org/data/2.5/weather?lat=35' +
              '&lon=139&appid=fece45cb56143f5b172b004c6f815f46');

      String data = response.body;
      print('------------ Status code : ${response.statusCode}');
      print('------------ Details : $data');
    } catch (e) {
      print(e);
    }
  }

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

the other file "locate" :


class Locate {
 double longitude = -18.5333;
 double latitude = 67.4462;

 Future<void> getLocate() async {
   try {
     Position position = await Geolocator.getCurrentPosition(
         desiredAccuracy: LocationAccuracy.low);

     longitude = position.longitude;
     latitude = position.latitude;
   } catch (e, s) {
     print(e);
     print(s);
   }
 }
} ```

Solution

  • it seems in lower androids like 4 and others like several devices it's not work , if you have same issue like me , try 4 step that i did and if you give error again , change your real or emu device