Your Second Flutter App

Nov 30 2021 · Dart 2.13, Flutter 2.2.3, Visual Studio Code

Part 1: Parse Network Data

04. Use a Model & Repository

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 03. Understand Futures Next episode: 05. Make a Network Call

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

As you progress in your career in mobile development, you'll gain an appreciation for the architecture of your app project. Architecture in this context means the structure of your code base, how different components in your code are connected and depend on one another. Now, common architectures and software are the MV Architectures. MVC, MVP, MVVM and more. The common M in these architectures stands for Model. The model represents the data or content in your app. It describes how you represent or model that data. All apps have data in content, which is why the model is common amongst all the various different architectures. The MV Architectures are primarily user interface architectures. That is for software that has a user interface component. The user interface corresponds to the V which stands for View. The view is what lets you see and interact with the app model. For more complicated apps, it's common to go beyond the MV Architectures and use architectures that go by the name Clean Architecture and VIPER. Now, a common component in all architectures is called a repository. A repository gives the rest of the app access to the apps data. Other components ask the repository for data when they need it. Now, repository classes typically are defined in terms of an interface, which in dart takes the form of an abstract class. In the future, when you learn more about app architectures, you'll learn that an interface is used because it helps with testing your app and also with ensuring your apps' components are connected in a efficient way. Now, in this episode, you'll get a start in working with architectures by creating a model class and a repository for the RW courses app. The main piece of data for RW courses is a course. So, we'll make a model class for the course. Create a new model folder. Once that's in place, create a new file called course.dart in that folder. Add the following. As you can see, the course just encapsulates some information about the course itself. Next, create a new folder in lib called repository. Then, create a new file in the repository folder called repository.dart. In the folder, add the following. This defines a single method for getting courses. Of course, we're getting an error because we're missing the course import. Add it now. You'll notice that we've defined a future. We're going to make a network request, which can take an unknown amount of time. This future will return a list of courses for us. We will also pass in the domain filter to specify what courses we want. This is just an abstract class. Now, we need a concrete implementation. Create a new course repository file in the repository folder. First, import the repository abstract class. Next, create a course repository class. We're getting an error because we aren't implementing the getCourses method. Don't worry. We'll get to that in a moment. First, let's define an API end point so we can get the course data. Now, to get a method to get our courses. This returns a future that contains a list of courses. Make sure to import the courses from the model folder. We'll need to access our Constants so import that file as well. Set the URL and add a filter parameter. In the next episode, with the repository in place, you'll see how to make a network call in flutter. Then, you'll use the course repository to make the call.