londonappbrewery/Clima-Flutter

Open Weather API crashing when getting data

Closed this issue · 1 comments

My code works perfectly with the sample link, but as soon as I try the API URL it crashes, I thought it was an error with the http package so I tried an alternative called Dio, but still crashes.

The correct URL is https://api.openweathermap.org/data/2.5/weather?lat=-38.115958&lon=176.2232279&appid=ab049c97d78636ee1e507a36cc400930 which works perfectly.

Here's my code:

import 'dart:convert' as convert;
import 'package:flutter/material.dart';
import 'package:clima/services/location.dart';
import 'package:dio/dio.dart';

const apiKey = 'ab049c97d78636ee1e507a36cc400930';

class LoadingScreen extends StatefulWidget {
  @override
  _LoadingScreenState createState() => _LoadingScreenState();
}

class _LoadingScreenState extends State<LoadingScreen> {

      double latitude;
      double longitude;

   void getLocation() async {

     Location currentLocation = Location();

     await currentLocation.getCurrentLocation();
     latitude = currentLocation.latitude;
     longitude = currentLocation.longitude;

   }

   void getData() async {
     var url = 'https://api.openweathermap.org/data/2.5/weather?lat=$latitude&lon=$longitude&appid=$apiKey';
     Response jsonResponse = await Dio().get(url);
      if (jsonResponse.statusCode == 200){

        var decodedData = convert.jsonDecode(jsonResponse.toString());

       int condition = decodedData['weather'][0]['id'];
       double temperature = decodedData['main']['temp'];
       String cityName = decodedData['name'];
       print(cityName);
       print(temperature);
       print(condition);
       print(latitude);
       print(longitude);



      } else {
        print(jsonResponse.statusCode);
      }
   }


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


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

Here is the error:

Performing hot restart...
Syncing files to device SM G955F...
Restarted application in 1,636ms.
E/flutter ( 9434): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: DioError [DioErrorType.RESPONSE]: Http status error [400]
E/flutter ( 9434): #0 DioMixin._dispatchRequest (package:dio/src/dio.dart:963:7)
E/flutter ( 9434):
E/flutter ( 9434): #1 DioMixin._request._interceptorWrapper... (package:dio/src/dio.dart:849:37)
E/flutter ( 9434): #2 DioMixin.checkIfNeedEnqueue (package:dio/src/dio.dart:1118:22)
E/flutter ( 9434): #3 DioMixin._request._interceptorWrapper.. (package:dio/src/dio.dart:846:22)
E/flutter ( 9434): #4 new Future. (dart:async/future.dart:176:37)
E/flutter ( 9434): #5 _rootRun (dart:async/zone.dart:1122:38)
E/flutter ( 9434): #6 _CustomZone.run (dart:async/zone.dart:1023:19)
E/flutter ( 9434): #7 _CustomZone.runGuarded (dart:async/zone.dart:925:7)
E/flutter ( 9434): #8 _CustomZone.bindCallbackGuarded. (dart:async/zone.dart:965:23)
E/flutter ( 9434): #9 _rootRun (dart:async/zone.dart:1126:13)
E/flutter ( 9434): #10 _CustomZone.run (dart:async/zone.dart:1023:19)
E/flutter ( 9434): #11 _CustomZone.bindCallback. (dart:async/zone.dart:949:23)
E/flutter ( 9434): #12 Timer._createTimer. (dart:async-patch/timer_patch.dart:23:15)
E/flutter ( 9434): #13 _Timer._runTimers (dart:isolate-patch/timer_impl.dart:384:19)
E/flutter ( 9434): #14 _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:418:5)
E/flutter ( 9434): #15 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:174:12)
E/flutter ( 9434):

Oh wow, I'm an idiot.. I didn't put the getData() inside of getLocation() - i put it in initState() - so I was getting null values for my langitude and longitude.

Update below.

  void getLocation() async {
    Location currentLocation = Location();

    await currentLocation.getCurrentLocation();
    latitude = currentLocation.latitude;
    longitude = currentLocation.longitude;

    getData();
  }