getItem method

Future<Item> getItem(
  1. int id
)

Fetches an Item that could be Story, Poll, Job, or Ask

Implementation

Future<Item> getItem(int id) async {
  try {
    Uri url = Uri.parse('$endpoint/item/$id.json');
    http.Response res = await _client.get(url);
    if (res.statusCode == 200) {
      Map<String, dynamic> json = jsonDecode(res.body);
      if (json['type'] == 'story') {
        return Item.story(Story.fromJson(json));
      }
      if (json['type'] == 'poll') {
        return Item.poll(Poll.fromJson(json));
      }
      if (json['type'] == 'ask') {
        return Item.ask(Ask.fromJson(json));
      }
      if (json['type'] == 'job') {
        return Item.job(Job.fromJson(json));
      }
    }
    throw HackerNewsClientFailure();
  } catch (e) {
    print('HackerNewsClient.getStory $e');
    rethrow;
  }
}