Build Your Weather Tracking Flutter App Using GetX | by Adem Gunay | Apr, 2022

Create cross-platform purposes simply obtained simpler

Picture by Tudor Baciu

So you have an interest in Flutter however you don’t know the way or the place to begin to construct your first app? Or, maybe, you have an interest in GetX? It’s your fortunate day! You simply discovered what you had been searching for 🙂

On this article, we’re going to discover the best way to use GetX state administration to construct our cross-platform Android and iOS cellular app with Flutter. Seize your espresso or every other favourite drink and let’s get began!

A Easy climate monitoring app with some extra info:

What you’ll study:

  1. Setup a Flutter venture
  2. Make HTTP requests to retrieve information from an API
  3. Carry out asynchronous duties
  4. Outline a strong and scalable base structure
  5. Handle information in a reactive means utilizing Get


I will likely be utilizing Android Studio IDE on this tutorial. If you happen to really feel extra snug with Visible Studio Code, it really works completely high quality as effectively. Take your choose!

We must obtain the Flutter SDK which could be discovered here. You’ll be able to observe the directions on that official web page, it’s clear and simple. As soon as set, run the command line flutter physician (as talked about within the documentation) to substantiate all the pieces went effectively.

If you happen to face points whereas doing the setup, be certain that to consult with their official documentation. You could find just about each resolution there.

From right here on, IDE-related directions will likely be for Android Studio. Visible Studio Code must be comparable sufficient.

To create a brand new Flutter venture, click on on File > New > New Flutter Challenge. Specify your SDK location and set your venture’s location. Make sure that Android and iOS are checked. You’ll be able to select the venture’s title 🙂

As soon as the venture is created, you’ll land on the textual content editor with a bunch of default code. It’s the code base of the well-known click on counter Flutter demo app. With a purpose to run the demo code, you’ll have to create an Android or iOS emulator first. As for the iOS emulator, you’ll need a Mac to run it.

For creating an Android emulator, click on on Instruments > AVD Supervisor > Create Digital System > Pixel 4 (for instance) > Select an Android Picture from Advisable > End

You now have a tool to run the applying on. Choose the system on the highest menu and click on Run.

After compiling efficiently, you ought to be working the Fluter demo click on Counter app in your emulator and get a show just like the picture under.

Click on counter app

You’ll be able to take a while to discover the totally different repositories. At this level, you’ll have 3 key repositories to grasp:

  • android/: this accommodates all the info associated to native Android. If there’s any Android particular setup to do, you’ll have to replace information on this repository. I.E: including permissions within the manifest, setting variations, updating Gradle or Kotlin variations, and many others.
  • ios/: this accommodates all the info associated to native iOS code. Any iOS particular setup is completed right here. I.E: organising targetOS, permissions, register configs, and many others.
  • lib/ : that is the place our Flutter-related code will dwell. Discover the preliminary fundamental.dart file which accommodates all of the demo code.

Time to code!

Keep in mind GetX? That’s our state administration package deal. It mainly helps us to make a reactive UI, and construction/set up our app’s code, making it simply scalable whereas lowering the boilerplate. To arrange a package deal akin to this one, we have to dive in pubspec.yaml file. That is the place our dependencies to 3rd celebration packages are set. Open the file and beneath dependencies: add the GetX package to the venture.

sdk: flutter
cupertino_icons: ^1.0.2
get: ^4.6.1

While you’re finished including this line, you’ll have to use the command line flutter pub get within the terminal inside the venture’s listing, which is able to obtain all the brand new dependencies code inside pubspec.lock.

In Android Studio, you may see a shortcut button on the high Pub get.

Go into fundamental.dart file and begin by deleting the entire content material. This file is the one we’re utilizing to run our app. The core perform to start out a Flutter app is the runApp() perform that may create our widget tree.

By default, Flutter makes use of the MaterialApp root widget however the GetX package deal requires to vary it into GetMaterialApp to benefit from the package deal’s options.

At this level, your file ought to seem like this:

Suggestions: in case you write stless or stful and press return key, Android Studio will robotically create the boilerplate code for, respectfully, StatelessWidget and StatefulWidget for you.

What is occurring in GetMaterialApp?

  • getPages: It will comprise all of the pages of our app. Every web page widget will likely be wrapped inside GetPage() object.
  • HomePage(): the UI for the foundation web page (additionally named: ‘/’).
  • HomeBinding(): accommodates all of the lessons the House web page must work appropriately (i.e: the controller, the repositories, and many others.). We are going to get into these in a while.

First, let’s outline a brand new HomePage widget utilizing stless shortcut. The primary widget for a brand new web page is often a Scaffold. We will even add some UI containing mock information to point out one thing. We will likely be utilizing Column to align ourText widgets containing some Strings vertically in addition to some icons to make it prettier.

Replace the HomePage widget to seem like this:

And add the home_binding.dart that we did see beforehand. This class must lengthen Bindings and we must overwrite the dependencies() perform. Briefly, it’s how we inject dependencies in get. Create the category with the next code:

import 'package deal:get/get.dart';class HomeBinding extends Bindings 
void dependencies()
// TODO: We are going to add the dependencies later.

⚠️ Make sure that to import these 2 new lessons into the fundamental.dart after which run the app and it’s best to have one thing like this in your emulator:

If you happen to look rigorously, you may see that our Row’s gadgets are repeated a number of occasions. We are going to extract the code right into a separate widget, in a while, to make it reusable and keep away from repetition.

You may also have seen the SafeArea widget. Its function is to outline the UI’s boundaries. Normally, units require spacing for the house button on the backside in addition to spacing on the high for standing bars.

The SafeArea widget

We now have our UI however we need to fetch some information to make it dynamic. Let’s create a brand new Controller class known as home_controller.dart contained in the lib/ folder.
The controller has to increase GetxController to benefit from the Get library.

Your controller will seem like this:

import 'package deal:get/get.dart';

class HomeController extends GetxController

// TODO add the logic to fetch present metropolis

// TODO add the logic to fetch temperature for present metropolis

We are going to want 2 information fashions at this level. The info mannequin will maintain town info and one other information mannequin that may maintain the climate info.

First, let’s perceive the best way to get the present metropolis info. We’re going to use the endpoint This request is giving us information based mostly on the consumer’s IP handle. This isn’t very correct because it relies upon on the web supplier’s location but it surely permits us to disregard location permission requests. An instance of utilizing Canadian IP handle can be:

"standing": "success",
"nation": "Canada", // We want this
"countryCode": "CA",
"area": "QC",
"regionName": "Quebec", // We want this
"metropolis": "Montreal",
"zip": "H3G",
"lat": 45.4995, // We want this
"lon": -73.5848, // We want this
"timezone": "America/Toronto",
"isp": "Le Groupe Videotron Ltee",
"org": "Videotron Ltee",
"as": "AS5769 Videotron Telecom Ltee",
"question": ""

Let’s create the mannequin known as location_data.dart that may maintain the situation info.

NOTE: The .fromJson perform’s function is to transform the JSON information we get from the HTTP request right into a dart object that we will use inside our app.

Our subsequent job is to make the http request inside our app. With a purpose to do this, we’d like the assistance of one other package deal known as http package deal, the official one from the Flutter workforce. ⚠️ Don’t neglect that each time we’d like a third-party library, we have to add it into our pubspec.yaml file adopted by a flutter pub get command to retrieve the package deal’s code.

sdk: flutter
cupertino_icons: ^1.0.2
get: ^4.6.1
http: ^0.13.4

As soon as we did add this package deal, we will use its code to make our HTTP requests to an endpoint and retrieve the JSON object.

Let’s add a repository that may do the decision and put together the info for our controller. Create a repository known as weather_repository.dart as under:

Word: This perform’s physique is wrapped inside async and returns a Future<LocationData?> sort. It means this operation will run asynchronously and can take a while to finish. It’ll return a nullable LocationData object sooner or later. The await tells the perform to cease and wait to get the endpoint’s information earlier than persevering with (the time consuming operation is that particular line).

Now, keep in mind the Binding that’s supposed to carry all the necessities for the UI to work appropriately? We HomePage goes to want WeatherRepository and the HomeController. We now have to instantiate these 2 objects contained in the dependencies perform. Let’s replace the Binding as observe:

import 'package deal:get/get.dart';
import 'package deal:my_weather_tracker/module/home_controller.dart';
import 'package deal:my_weather_tracker/repository/weather_repository.dart';

class HomeBinding extends Bindings
void dependencies()
Get.lazyPut(() => WeatherRepository());
Get.lazyPut(() => HomeController());

Get.lazyPut(() => abc()) is a perform that may create an occasion of the thing each time it’s required. For instance, after we make a name upon their strategies, if the thing doesn’t exist but, we’ll create one. If it was already created, we’ll use the present one.

Now we will import this repository into our controller and use getCurrentLocation perform inside it. Replace your controller like this:

NOTE: The Rxn object is an observable however with nullable sort. On this case, since we won’t know the situation till we make the request, it’s initially set to null. Extra Rx varieties here.

Now, all we’ve got to do is to replace our UI with the retrieved worth. We now have to get an occasion of this controller inside the home_page.dart file and observe the modifications on locationData to show the consumer’s present metropolis.
Add this line beneath the HomePage() constructor:

last HomeController _controller =;

Word: the prefix underscore on the variable’s title makes it non-public.

To make our UI reactive to our controller’s modifications, we have to wrap the SafeArea widget with an Obx widget. It will notify and replace SafeArea‘s content material each time an observable’s worth modifications.

little one: Obx(
() => SafeArea(
little one: Column(

Now we will add the controller’s handle variable within the Textual content. Since handle is referencing an Rx observable, the Obx wrapper larger within the widget tree will get triggered on its worth change and it’ll notify the UI to recreate the widget with the brand new worth.

// We are going to fetch the consumer's present metropolis
little one: Textual content(
"Your location is $_controller.handle",
type: const TextStyle(fontSize: 18),

NOTE: in case you get any error, attempt eradicating some const key phrase as they can’t be constants anymore.

Attempt working your app and it’s best to see town now!

We have to know the temperature at this level and to get that information, we’re going to use Open Weather Map API. Make sure that to create an account to get a free API key. You will have it to make HTTP requests.

Here’s a snippet of the info response and the properties we’ll want from that API name:

"temp": 280.58, // What we'd like, default unit is Kelvin.
"feels_like": 280.03,
"temp_min": 278.5,
"temp_max": 281.33,
"strain": 1011,
"humidity": 84

Let’s create a WeatherData class to comprise weather-related info:

And now, to retrieve this information, we’ll add this new getWeatherForLocation perform into our WeatherRepository. The API will request some extra information akin to, latitude, longitude, and many others. that we’re going to go inside params. We are going to parse the JSON response right into a WeatherData object.

Now we have to faucet into it to retrieve the temperature in our controller.

Lastly, we’ve got to make use of this new temperature variable inside the UI Widget HomePage:

// We are going to use town information to fetch the climate
little one: Textual content(
"The temperature is $_controller.temperature°C",
type: const TextStyle(fontSize: 18),

Run the app and tada 🎉

We are going to full the final row merchandise, which is an informative textual content based mostly on the climate. In fact, that ought to rely upon a number of components (humidity, strain, max and min temperatures, and many others.) however for simplicity’s sake, let’s use solely temperature. We may have the next 4 eventualities:

  1. temperature ≤ 0 → “be certain that to decorate thick cloths! It’s freezing on the market!”
  2. temperature ≤ 15 → “put on a jacket, don’t catch a chilly!”
  3. else → “benefit from the climate, it’s good!”
  4. temperature is null → “unknown”
/// inside HomeController
_getInfoText(int? temperature)
if (temperature == null)
infoText.worth = "unknown";
else if (temperature <= 0)
infoText.worth =
"be certain that to decorate thick cloths! It is freezing on the market!";
else if (temperature <= 15)
infoText.worth = "put on a jacket, do not catch a chilly!";
infoText.worth = "benefit from the climate, it is good!";

Replace the HomeController with a brand new variable as observe and add it to the widget.

///...// A reactive String to show informative textual content. default is '...'.
RxString infoText = '...'.obs;
///...getTemperatureForCurrentLocation() async
// Confirm if location shouldn't be null first
if (locationData.worth != null)
// We assign the response from our API name to our Rx object.
weatherData.worth =
await _repository.getWeatherForLocation(locationData.worth!);
_getInfoText(weatherData.worth?.temp); // make the decision right here

The final job is to replace our UI with a brand new observable and that’s it!

// We are going to give recommendation on what to do
little one: Textual content(
"It's best to $_controller.infoText.worth",
type: TextStyle(fontSize: 18),

Let’s see the end result!

The very last thing right here is to refactor our row widgets. We are able to clearly discover a sample in every of them. A number one Icon and a trailing Textual content inside a Container. We are going to create our personal RowItem widget which will likely be an assembling of the beforehand listed widgets.

Significantly better and extra readable proper? All the time keep in mind to attempt to extract identical patterns into a unique widget. You’ll be able to even make these widgets world by creating a brand new file containing a StatelessWidget known as RowItem. It will will let you reuse that row customized widget anyplace inside the app.

That StatelessWidget RowItem would seem like this:

Attempt changing _rowItem with this new RowItem and you will notice that it really works precisely the identical 😉

icon: Icons.thermostat_outlined,
textual content: "The temperature is $_controller.temperature°C",

Be at liberty to take a look at the entire venture which is out there on my GitHub 🚀

More Posts