Previous episode: 07. Challenge: Add More Properties
Next episode: 09. Use Dependency Injection
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.
Each course has a domain like iOS or Android. And some courses are for multiple domains. Domain parsing is a bit more involved than other course properties since each course can have multiple domains. In this episode, you'll see how to parse the list of domains in the fromJson method. Look at the raw Json in the browser. We want to check out the domain value. We can find that under relationships. The domains come with a data object that contains an ID and a type. This is what we'll be parsing. To get started, open your project in progress or download the starter project. Create a new file under the model folder. Call it domain.dart. We'll start by adding an enumeration for all the different domains currently supported. We'll also include an all and unknown and an archived value. Next, we'll add an extension to the enumeration. This extension will make it quite easy to get a String value. We're getting some compile errors because we haven't imported the String's value. Let's add it now. With the domains all set, let's head on over to our course class and add a domain property to it. Open up course.dart and add a list property to the class. Now, import the domain file. Next, update the constructor to take in the domains. Now for the fun part, we need to update the fromJson constructor method. Remember the Json is passed into this constructor, so we have to configure it to work with the course object. Scroll down to the fromJson constructor. Add the domains. We'll add a function property to parse the domains. First, we'll get the domainData from the Json. Now, if the domainData has a length greater than zero we'll loop through the contents and add it to the domains list. So, we've gotten the idea of the domain and we pass it into the getDomain method. We haven't defined it. Let's import Constants. Now, let's write the getDomain method. We're going to write a getter that will loop through the domain list and add it to a list. We've added some additional logic to make sure our printed list doesn't have a trailing comma at the end of it. Finally, to test it out, we'll update our toString method to print out the domainString. Nice work. Now, HUD reload or build and run. This time, you'll see a list of courses with the domainString. Well done.
All videos. All books.
One low price.
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.