fetchPosts method

Future<List<Post>> fetchPosts({
  1. SortType sortType = SortType.Hot,
  2. required int page,
  3. required int quantity,
})

Gets and returns a list of Posts in this guild

Implementation

Future<List<Post>> fetchPosts({
  SortType sortType = SortType.Hot,
  required int page,
  required int quantity,
}) async {
  // Get posts from listing, determining how the posts should be sorted
  // and which page the listing is to be extracted from
  final response = await api.get('guild/$name/listing', headers: {
    'sort': EnumToString.convertToString(sortType).toLowerCase(),
    'page': page,
  });

  if (response == null) {
    return [];
  }

  final body = jsonDecode(response.body);

  // Converts _InternalLinkedHashMap<String, dynamic> to a List<dynamic> with all the posts
  List<dynamic> rawPosts = Map<String, dynamic>.from(body)['data'];

  return rawPosts.map((rawPost) => Post(api)..fetchData(rawPost)).toList();
}