SearchResult.fromJson constructor

SearchResult.fromJson({
  1. required dynamic json,
  2. ResultType type = ResultType.itunes,
})

Implementation

factory SearchResult.fromJson(
    {required dynamic json, ResultType type = ResultType.itunes}) {
  /// Did we get an error message?
  if (json['errorMessage'] != null) {
    return SearchResult.fromError(
        lastError: json['errorMessage'] ?? '',
        lastErrorType: ErrorType.failed);
  }

  var dataStart = startTagMap[type];
  var dataCount = countTagMap[type];

  /// Fetch the results from the JSON data.
  final items = json[dataStart] == null
      ? null
      : (json[dataStart] as List)
          .cast<Map<String, dynamic>>()
          .map((Map<String, dynamic> item) {
          return Item.fromJson(json: item, type: type);
        }).toList();

  return SearchResult(
      resultCount: json[dataCount] ?? 0, items: items ?? <Item>[]);
}