extractData static method

Map<String, dynamic> extractData(
  1. Map<String, dynamic> dataMap
)

Used to extract the desired data from a raw JSON data map response.

Example

/// Raw response
{
  "errors": [],
  "data":
  [
    {
      "id": ...
      ...
    }
  ]
}

/// Desired JSON data
{
  "id": ...
  ...
}

Implementation

static Map<String, dynamic> extractData(Map<String, dynamic> dataMap) {
  Map<String, dynamic> data;

  if (dataMap[ApiFields.data] != null && dataMap[ApiFields.id] == null) {
    try {
      data = Map<String, dynamic>.from(dataMap[ApiFields.data]);
    } catch (_) {
      List<Map<String, dynamic>> resourceList =
          List<Map<String, dynamic>>.from(dataMap[ApiFields.data]);

      try {
        data = resourceList.first;
      } catch (__) {
        data = {};
      }
    }
  } else {
    data = dataMap;
  }

  return data;
}