getPhotos method

Future<String> getPhotos({
  1. int? limit,
  2. int? id,
})

Return a list of photos

limit is the limit of photos to return

id is the id of a particular photo to return

Implementation

Future<String> getPhotos({int? limit, int? id}) async {
  try {
    if (id == null) {
      var response = await http.get(
        Uri.parse("$baseUrl/photos"),
      );
      List<dynamic> posts = jsonDecode(response.body);
      if (limit != null) {
        posts = posts.sublist(0, min(limit, posts.length));
      }
      return jsonEncode(posts);
    } else {
      var response = await http.get(
        Uri.parse("$baseUrl/photos/$id"),
      );
      return response.body;
    }
  } catch (e) {
    throw Exception(e.toString());
  }
}