getTopStories method
Fetches the top 10 (by default) Hacker News Stories
Implementation
Future<List<Item>> getTopStories({int storyCount = 10}) async {
try {
Uri url = Uri.parse('$endpoint/topstories.json');
http.Response res = await _client.get(url);
if (res.statusCode == 200) {
List<int> ids = List<int>.from(jsonDecode(res.body));
ids.removeRange(storyCount, ids.length);
List<Item> items = await Future.wait(ids.map((int id) async {
Item item = await getItem(id);
return item;
}).toList());
return items;
}
throw HackerNewsClientFailure();
} catch (e) {
print('HackerNewsClient.getTopStories $e');
rethrow;
}
}