getTodos method

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

Return a list of todos

limit is the limit of todos to return

id is the id of a particular todo to return

Implementation

Future<String> getTodos({int? limit, int? id}) async {
  try {
    if (id == null) {
      var response = await http.get(
        Uri.parse("$baseUrl/todos"),
      );
      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/todos/$id"),
      );
      return response.body;
    }
  } catch (e) {
    throw Exception(e.toString());
  }
}