Home > Back-end >  Cod 400 , message: wrong latitude, OpenWearher: getting current weatherData
Cod 400 , message: wrong latitude, OpenWearher: getting current weatherData

Time:04-16

I'm getting error (wrong latitude) when I'm trying to get current weather data. can anyone tell me what went wrong I tried changing locationAccuracy from low to best but it didn't change anything

Location location = Location();
class _LoadingScreenState extends State<LoadingScreen> {
  String weatherABI = '4ead5f41d1c622302dd34242f9d1c25a';
  void getlocation() async {
    await location.getCurrentLocation();
    print(location.latitude);
    print(location.longtiude);
  }

  void getData() async {
    Uri url = Uri.parse(
        'https://api.openweathermap.org/data/2.5/weather?lat=${location.latitude}&lon=${location.longtiude}&appid=$weatherABI');
    Response response = await get(url);
    print(response.body);

    if (response.statusCode == 200) {
      String data = response.body;
      print(data);
    } else {
      print(response.statusCode);
    }
  }

  @override
  void initState() {
    super.initState();
    getlocation();
    getData();
  }

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

the error : enter image description here

CodePudding user response:

Just call your getData() function in the end of getLocation() function because both functions called at same time that's why getData() function does not get any location data. You need to get location first then you need to call getData function.

Remove getData() function from initState()

  void getlocation() async {
   await location.getCurrentLocation();
   getData();
   print(location.latitude);
   print(location.longtiude);
  }

You also need to call setState in getData() function once you retrieved data from the API to update your UI.

  • Related