fromJson method

dynamic fromJson(
  1. Map<String, dynamic> data, {
  2. List<Map<String, dynamic>>? withIncluded,
})

Takes a full JSON:API Response Object (the contents of a "data" field) will clear and update id, apiPath, attributes and _relationships

Implementation

fromJson(Map<String, dynamic> data,
    {List<Map<String, dynamic>>? withIncluded}) {
  if (data['type'] != resourceType) {
    throw FormatException(
        'Incorrect data type: ${data['type']} given, but $resourceType expected.');
  }

  // responses will always have an id, but we shouldn't update this data
  // if the response data has a different id!
  assert(id == null || data['id'] == id);

  // update this id if needed
  _id ??= data['id'];

  // update the attributes
  if (data.containsKey('attributes')) {
    _attributes
      ..clear()
      ..addAll(data['attributes']);
  }

  // update the links
  if (data.containsKey('links')) {
    _links.addAll(data['links']);
  }

  // process relationships, but first ensure
  // all relationship objects are actually lists
  if (data.containsKey('relationships')) {
    _relationships.clear();
    // parse each relationship into its relevant object
    _relationships.addAll(
        handleRelationships(data['relationships'], withIncluded ?? []));
  }

  // // and process additional included items
  // if (withIncluded != null) {
  //   handleIncludes(withIncluded);
  // }
}