searchById function

Future<Cocktail> searchById(
  1. int i
)

Search cocktail details by id.

  • i Cocktail id.

Returns Cocktail.

Implementation

Future<Cocktail> searchById(int i) async {
  try {
    var response = await _getRequest("lookup.php?i=${i}");
    if (response.length == 0) {
      throw "no results found";
    }
    var json = jsonDecode(response);
    if (json["drinks"] == null || json["drinks"].length == 0) {
      throw "no results found";
    }
    var data = Cocktail.fromJson(json["drinks"][0]);
    return data;
  } catch(ex) {
    throw new CocktailDBException(ex.toString());
  }
}