search function

Future<List<Cocktail>> search(
  1. String s
)

Search cocktail by name.

  • s Cocktail name.

Returns List of Cocktail.

Implementation

Future<List<Cocktail>> search(String s) async {
  try {
    var response = await _getRequest("search.php?s=${s.trim()}");
    if (response.length == 0) {
      throw "no results found";
    }
    var json = jsonDecode(response);
    if (json["drinks"] == null || json["drinks"].length == 0) {
      throw "no results found";
    }
    List<Cocktail> list = [];
    for (var i in json["drinks"]) {
      list.add(Cocktail.fromJson(i));
    }
    return list;
  } catch(ex) {
    throw new CocktailDBException(ex.toString());
  }
}