pratteek shaurya
pratteek shaurya

Reputation: 960

Getting statusCode error on string interpolation

Below is my code:

const apiKey = 'XXXXXXXXXXXXXXXXXXXXXXXXXX';

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

class _LoadingScreenState extends State<LoadingScreen> {
  double latitude;
  double longitude;

  @override
  void initState() {
    super.initState();

    getLocation();
    print('code started');

    getData();
  }

  void getLocation() async {
    Location location = Location();
    await location.getCurrenctLocation();
    print(location.latitude);
    print(location.longitude);

    latitude = location.latitude; // SAVING LATITUDE POSITION HERE
    longitude = location.longitude;  // SAVING LONGITUDE POSITION HERE
  }

  void getData() async {
    http.Response response = await http.get(
        'https://api.openweathermap.org/data/2.5/weather?lat=25&lon=85&appid=$apiKey');

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

      var decodedData = jsonDecode(data);

      double temperature = decodedData['main']['temp'];
      int condition = decodedData['weather'][0]['id'];
      String cityName = decodedData['name'];

      print(temperature);
      print(cityName);
      print(condition);
      
    } else {
      print(response.statusCode);
    }
  }

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

Right now I am getting this output:

flutter: code started
flutter: 25.5
flutter: 85.1
flutter: 300.15
flutter: Tekāri

Problem arises when I make change in the URL

Before Change: https://api.openweathermap.org/data/2.5/weather?lat=25&lon=85&appid=$apiKey

After change: https://api.openweathermap.org/data/2.5/weather?lat=$latitude&lon=$longitude&appid=$apiKey

After I make this change my output is:

flutter: code started
flutter: 25.5
flutter: 85.1
flutter: 400

Which means my statusCode is 400, and there is some error. But before making URL change my position.latitude and position.longitude does gets prints, after that the only thing which I did was save these two in a variable pass it on the URL. I cant understand why my statusCode turns to 400.

Upvotes: 0

Views: 106

Answers (1)

user14280337
user14280337

Reputation:

I do not think this error has something to do with string interpolation but with variable initialisation.

Looking at your code, I do not think the variables latitude and longitude are initialised when the function 'getData()' is called.

You should call the function 'getData()' from within the 'getLocation()' function and remove it from 'initState()'. Like this:

void getLocation() async {
    Location location = Location();
    await location.getCurrenctLocation();
    print(location.latitude);
    print(location.longitude);

    latitude = location.latitude; // SAVING LATITUDE POSITION HERE
    longitude = location.longitude;  // SAVING LONGITUDE POSITION HERE

    getData();
  }

Let me know if it works!

Upvotes: 1

Related Questions