Learn About Flutter Widgets by Making a Simple Weather App
By: Madhav Bahl
Tags: Android, Flutter, Mobile, iOS, Programming

Step 4. Add Location Logic

Now that you already know how to include Flutter libraries in your project, this one’s going to be pretty simple. We are going to use the location library.

Again, the first step is to include the dependency in pubspec.yaml and then pub get .

location: ^3.0.0

Now let’s create a new class LocationHelper to hold fetch and hold our location data. I am going to do that in utils folder — location.dart

Here’s the code for location.dart . Notice that I have taken most of the fetch location logic from the Example section in the documentation of the library, and that’s how we start using libraries ;).

Now we need to call this fetch location logic from the loading_screen.dart , but before that, did you notice that we used some new keywords here?

async and await

If you are familiar with programming and making API calls in other languages, you can skip this and directly read the code. But if you are new, do give it a read.

You can read everything about async and await here, but I will try to summarize it for our use case.

Making API calls is not as straightforward as doing some arithmetic operations or any other sync logic. The thing with APIs is that we know when we make a call, but we don’t know the exact time that the API will take to fetch the results for us.

So instead of having a concrete result when we make an API call, what we have is an object of type Future which is similar to a promise that this object will have some value in the near future (it can be an error message if the API fails or the data we want if it passes).

Whenever we want to make our code wait for that Future object to have some concrete data before going further, we use the keyword await .

Each method/function which is waiting for the Future data (or using await ) has to be an async method. A simple example can be the following:

void getWeather() async {

weather = await makeApiCallForWeather();

... do something else ...

}

I hope it’s clear now. Let’s move on with the coding part and add the fetch location logic inside our loading screen.

Fetching the location behind the scenes when the app loads

In the loading screen, let’s add an object of LocationHelper and use it to fetch the location inside a new method getLocationData() .

Also, to test it out, let’s use the initState method which is available inside the stateful widgets to call the getLocationData . Note that for any stateful widget, initState gets triggered whenever we render the widget to the screen. (BTW, don’t forget to import the location helper class.)

LocationHelper locationData; Future<void> getLocationData() async {

locationData = LocationHelper();

await locationData.getCurrentLocation(); if (locationData.latitude == null || locationData.longitude == null) {

// todo: Handle no location

} print(locationData.longitude);

print(locationData.latitude);

} @override

void initState() {

// TODO: implement initState

super.initState();

getLocationData();

}

Woah, that was easy. Let’s check it out, and yes, the app now asks for the location permission.

And check out the console: It also prints your location now since we added the print statements in the getLocationData method.

Here’s the code for the loading_screen.dart so far:

With this, we are done with our fourth step as well, and here comes the fifth step.