AppStoreGenericResponse<T1 extends Serializable, T2 extends Serializable>.fromMap constructor Null safety

AppStoreGenericResponse<T1 extends Serializable, T2 extends Serializable>.fromMap(
  1. T1 itemParser(
    1. Map map
    ),
  2. Map json,
  3. {T2 includedParser(
    1. Map map
    )?}
)

A generic response from the App Store Connect API. A parser for the App Store Connect API response. Accepts a JSON Map and returns a AppStoreGenericResponse object.

Implementation

factory AppStoreGenericResponse.fromMap(
  T1 Function(Map<dynamic, dynamic> map) itemParser,
  Map<dynamic, dynamic> json, {
  T2 Function(Map<dynamic, dynamic> map)? includedParser,
}) {
  // make sure we're dealing with a JSON map
  json = json.cast<String, dynamic>();

  // extract the results from the map
  late final List<Map<String, dynamic>> items;
  List<Map<String, dynamic>>? included;

  final mapData = json['data'];

  if (mapData is List) {
    items = mapData
        .cast<Map<dynamic, dynamic>>()
        .map((Map<dynamic, dynamic> item) => item.cast<String, dynamic>())
        .toList();
  } else if (mapData is Map) {
    items = [mapData.cast<String, dynamic>()];
  } else {
    items = [];
  }

  final mapIncluded = json['included'];

  if (mapIncluded is List) {
    included = mapIncluded
        .cast<Map<dynamic, dynamic>>()
        .map((Map<dynamic, dynamic> item) => item.cast<String, dynamic>())
        .toList();
  } else if (mapIncluded is Map) {
    included = [mapIncluded.cast<String, dynamic>()];
  }

  // parse the results into a list of objects
  final parsedItems = items.map(itemParser).toList();
  List<T2>? parsedIncluded;

  if (includedParser != null) {
    // parse the results into a list of objects
    parsedIncluded = included?.map(includedParser).toList();
  }

  return AppStoreGenericResponse(
    meta: Meta.fromMap(json['meta'] as Map? ?? {}),
    items: parsedItems,
    included: parsedIncluded,
  );
}