fetch method

Future<List<NBResponse>> fetch({
  1. String? endpoint = null,
  2. int amount = 1,
})

Fetch a random instance of a endpoint from the api

Arguments endpoint and amount are both optional. Amount defaults to 1 and endpoint defaults to a randomly generated one.

Implementation

Future<List<NBResponse>> fetch(
    {String? endpoint = null, int amount = 1}) async {
  if (amount > 20) throw NBArgumentError("Amount cannot be greater than 20");
  if (amount < 1) throw NBArgumentError("Amount cannot be less than 1");
  if (endpoint is String) {
    endpoint = endpoint.toLowerCase();
    isValid(endpoint);
  } else {
    endpoint = randomCat();
  }

  var res = await requestJson("$endpoint?amount=$amount")
      .then((value) => value['results']);
  var arr = <NBResponse>[];
  res.forEach((element) {
    arr.add(NBResponse(element));
  });
  return arr;
}