Your next career begins with
Save 50% off your seat in our next iOS Bootcamp. Limited time only. Sessions start April 3.
Your Second Flutter App
Nov 30 2021 Dart 2.13, Flutter 2.2.3, Visual Studio Code
Part 1: Parse Network Data
8. Parse Domains

Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
About this episode
See how to parse more complicated data returned from the API, a list of domains associated with each course.
Instructors
Contributors
Instructor
Tech Editor
Illustrator
Nicolai Martelle Manaloto
Video Editor
Over 300 content creators. Join our team.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Watch this episode for free — or access the full course as a Kodeco Professional Subscriber. Learn more here
This video Parse Domains was last updated on Nov 30 2021
Each course has a domain like iOS or Android, and some courses are for multiple domains. Domain parsing is a bit more involved than the 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 raw JSON in browser. We want to check out the domain value. You can find that under relationships. The domains come with a data object that contains an id and 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 will start by adding an enumeration for all the different domains currently supported. We’ll also include an all and unknown value.
enum Domain { all, ios, android, flutter, sss, unity, macos, archived, unknown }
Next, we’ll add an extension on the enumeration. This extension will make it quite easy to get a string value.
extension DomainExtension on Domain {
String get name {
switch (this) {
case Domain.ios:
return Strings.ios;
case Domain.android:
return Strings.android;
case Domain.unity:
return Strings.unity;
case Domain.sss:
return Strings.sss;
case Domain.flutter:
return Strings.flutter;
case Domain.macos:
return Strings.macos;
case Domain.archived:
return Strings.archived;
default:
return Strings.unknown;
}
}
}
We’re getting compile errors because we haven’t imported the strings file. Lets add it now.
import '../strings.dart';
With the domains all set, lets head over to the course class and add a domain property to it. Open up course.dart
and add a list property to the class.
final List<Domain> domains;
Now import the Domain file.
import 'domain.dart';
Next, update the constructor to take in the domains.
this.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.
domains = []
We’ll add a function body to parse the domains. First we’ll get the domain data from the json.
domains = [] {
final domainData =
json['relationships']['domains']['data'] as List<dynamic>;
}
Now if the domain data has a length greater than zero, we’ll loop through the contents and add it to the domain’s list.
if (domainData.length > 0) {
for (var i = 0; i < domainData.length; i++) {
final domain =
Course.getDomain(json['relationships']['domains']['data'][i]['id'] as String);
domains.add(domain);
}
}
So we’ve gotten the id of the domain, and we pass into the getDomain method. We haven’t defined it. First let’s import Constants.
import '../constants.dart';
Now lets write the getDomain method.
static Domain getDomain(String domainId) {
switch (domainId) {
case Constants.iosDomain:
return Domain.ios;
case Constants.androidDomain:
return Domain.android;
case Constants.unityDomain:
return Domain.unity;
case Constants.sssDomain:
return Domain.sss;
case Constants.flutterDomain:
return Domain.flutter;
case Constants.macosDomain:
return Domain.macos;
case Constants.archivedDomain:
return Domain.archived;
default:
return Domain.unknown;
}
}
Now we’re going to write a getter that will loop through the domains list and add them to a list.
String get domainsString {
var result = '';
for (var i = 0; i < domains.length - 1; i++) {
result += domains[i].name + ', ';
}
result += domains.last.name;
return result;
}
We added some additional logic to make sure our printed list doesn’t have a trailing comma at the end of it. Finally to test this out, we’ll update our toString method to print out the domain’s string.
String toString() {
return name + ': ' + domainsString;
}
Nice work. Now hot reload or build and run. This time, you’ll see a list of courses with the domain’s strings. Well done.
All videos. All books.
One low price.
A Kodeco subscription is the best way to learn and master mobile development — plans start at just $19.99/month! Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.
Learn more