searchIngredientById function

Future<Ingredient> searchIngredientById(
  1. int i
)

Search ingredient by ID.

  • i Ingredient ID.

Returns Ingredient.

Implementation

Future<Ingredient> searchIngredientById(int i) async {
  try {
    var response = await _getRequest("lookup.php?iid=${i}");
    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());
  }
}