Pagination.fromJson constructor

Pagination.fromJson(
  1. Map<String, dynamic> json, {
  2. ObjectTypes? staticType,
})

Map a new pagination instance from a json map.

Implementation

factory Pagination.fromJson(Map<String, dynamic> json,
    {ObjectTypes? staticType}) {
  Pagination pagination = Pagination(
      hasMore: json['has_more'] ?? false, nextCursor: json['next_cursor']);

  // Extract the type of the list
  List listOfUnknowns =
      json['results'] != null ? json['results'] as List : [];
  if (listOfUnknowns.length > 0) {
    ObjectTypes autoType =
        stringToObjectType(listOfUnknowns.first['object'] ?? '');

    // Map the corresponding list accord to the type
    ObjectTypes object =
        autoType == ObjectTypes.None ? staticType ?? autoType : autoType;
    if (object == ObjectTypes.Block) {
      List<Block> blocks = Block.fromListJson(json['results'] as List);
      pagination._blocks = [...blocks];
    } else if (object == ObjectTypes.Database) {
      List<Database> databases = List<Database>.from(
          (json['results'] as List).map((e) => Database.fromJson(e)));
      pagination._databases = [...databases];
    } else if (object == ObjectTypes.Page) {}
  } else {
    pagination.isEmpty = true;
  }

  return pagination;
}