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

Episode complete

Play next episode

About this episode
See versions
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 7. Challenge: Add More Properties Next episode: 9. Use Dependency Injection

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.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;
        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.,

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);

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:
    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;
      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 +=;
  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.