Building a Weather Application with Dart's Flutter Framework (Part 2, Creating a Repository and Model)

in #utopian-io6 years ago (edited)

flutter-logo.jpg

What Will I Learn?

Basic Ideas
  • You will learn how to use the named constructors to interface with the generated code
  • You will learn how to setup a repository for location data and requested data
  • You will learn how to navigate multiple Dart objects to select the fields and items that you want to display

Requirements

System Requirements:
OS Support for Flutter:
  • Windows 7 SP1 or later (64-bit)
  • macOS (64-bit)
  • Linux (64-bit)

Required Knowledge

  • A basic understanding of APIs
  • A fair understanding of Mobile development and Imperative or Object Oriented Programming
  • Basic knowledge of how to read JSON data

Resources for Flutter and this Project:

Sources:

Flutter Logo (Google): https://flutter.io/

Difficulty

  • Intermediate

Description

This video tutorial is a continuation of the Flutter Mobile Weather Application tutorial series detailed in this video article.

Outline and Overview:

General Overview

The goal of this Video Tutorial Series is showcase a Weather Application with a reactive interface that makes use of the Geolocation Plugin to ping the GPS of the user's device. Once we have the location data, we can then make a request to the Open Weather Map's API using our API key. The resulting JSON response is then serialized by the built generated Json serializer code which was created using the JSON Annotation and JSON Serialization plugins. We select specific fields in this JSON request and display them to the view. The application also will use a few ReactiveX libraries like RxCommands to reactively respond to the user's input and the various data events through the use of Observables and Streams.

Outline of this Tutorial

We build on the JSON serializer code that was made in the last tutorial. We create a data model for the fields that we want to display to application's view and a repository for fetching the GPS data and the JSON data.

Item 1: Data Model

The Data model features a set of parameters and items that we want to be able to selectively pull from the JSON request and serve to the user. We do this by orientating our data around the highest level object that is featured in our desired data fields. In this case, the top level object is the City object. We can then create a named constructor for our model which will allow us to fetch the data in relation to this City object.

WeatherModel-constructor.jpg

In this image you can see the named constructor code for the data model. Notice how we pass in a City object and then use it as a baseline to reference the other objects that are connected to it.

Item 2: Http Repository

After deciding how we want our model to be structured, we can then implement logic which will allow us to actually make the requests to Open Weather Map's API. The resulting JSON request is then serialized into Dart objects and then pushed into our Data Model object. We are also able to modify the request based on various different constants. These modifications include the Location of the Device and how much data we can import through our JSON requests.

update-weather.jpg

This image shows the main function that is used to make the HTTP request and populate our model with data; the updateWeather function. Notice that we use string interpolation to modify the request with a Location Result data type. There is also a fallback url which contains a static set of latitudes and longitudes should the program not have access to the location data. The return type for this function is a Future with a List of Weather Model inside of it. This will be very important for when we turn the emitted data into an Observable.

Item 2: Location Repository

With our Http Repository built, we now need to gain access to the GPS of the device. To accomplish this, we make use of the Geolocation plugin for Flutter. This plugin uses Channel Methods to talk to platform specific GPS APIs which allows us to use the resulting data in our Flutter code. We are able to make a small function using a method called lastKnownLocation to get the GPS location from the user's device. We can also build a small utility function to check to see if the user has given us permissions to access their GPS.

location-functions.jpg

Above, we have the two location functions. The updateLocation function is a wrapper around the lastKnownLocation method. It lets us return a Location Result type wrapped in a Future which will be important when we implement the reactive elements and streams for this application. The getGps function is a utility function and it will be used as the basis for a Boolean Observable. If this observable returns a false value, then our updateLocation function can not be executed by the user or the application.

Project GitHub Repository:

The Source Code for this project can be found here

Video Tutorial

Projects and Series

Related Videos
Stand Alone Projects:
Building a Calculator
Movie Searcher Application

Minesweeper Game

Curriculum



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Hi @tensor,
I am really glad to see you on your path towards success. You are going great!
Do you remember me? We talked on facebook, and I recommended your post to curie. Then, I deleted my FB...

I wish you more success :)

Yes, I absolutely do remember. You really did help me out quite a bit back then; I had no idea what I was doing and now I feel like my content is becoming much more solid overall. Glad to see you are doing alright.

Hey @tensor! Thank you for the great work you've done!

We're already looking forward to your next contribution!

Fully Decentralized Rewards

We hope you will take the time to share your expertise and knowledge by rating contributions made by others on Utopian.io to help us reward the best contributions together.

Utopian Witness!

Vote for Utopian Witness! We are made of developers, system administrators, entrepreneurs, artists, content creators, thinkers. We embrace every nationality, mindset and belief.

Want to chat? Join us on Discord https://discord.me/utopian-io

Really like the new system and I've finally been able to afford delegating some SP over to the community thanks to your votes on my contributions from the last 3-4 months.

Great work @tensor. I appreciate that rather than provide shortcuts you layout the code in a thorough manner for learners. I also appreciate that you identify any errors that arise and show solutions. Well done.
Cheers


Need help? Write a ticket on https://support.utopian.io.
Chat with us on Discord.

[utopian-moderator]

Thank you for the compliments and of course for moderating my content, if you can think of any suggestions with regards to my new article format, let me know. I am also working on learning more about video editing (I am a total newb), so that I can bring up the overall production value of my videos.

Cheers.

Cool tutorial @tensor, I am waiting for the next flutter episodes :)

Thank you. Ive got a bunch in the pipeline.

Coin Marketplace

STEEM 0.25
TRX 0.11
JST 0.033
BTC 63282.43
ETH 3083.92
USDT 1.00
SBD 3.85