searchIngredient function

Future<Ingredient> searchIngredient(
  1. String s
)

Search ingredient by name.

  • s Ingredient name.

Returns Ingredient.

Implementation

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