handleIncludes method

void handleIncludes(
  1. List<Map<String, dynamic>> included
)

This function populates the relationships with typed resource objects based on the data in the included map of the json.

This assumes that the relationship object has already been populated

Implementation

void handleIncludes(List<Map<String, dynamic>> included) {
  // make a quick mapping for later reference into nested objects
  Map<String, PcoResource> relDataMap = {};
  _relationships.forEach((key, items) {
    for (var item in items) {
      if (item.id == null) continue;
      relDataMap[item.id! + '-' + item.resourceType] = item;
    }
  });

  _included.clear();
  // pair the included items with the previously defined relationships
  for (var data in included) {
    if (data['type'] == null || data['id'] == null) continue;
    var res = handleItem(data);
    if (res == null) continue;
    _included.add(res);

    var key = data['id'] + '-' + data['type'];

    // we call .fromJson to modify the existing object in the _relationships Map
    // but only if the relationship was already mapped.
    if (relDataMap.containsKey(key)) relDataMap[key]!.fromJson(res.toJson());
  }
}