listComments method

Future<List<Comment>> listComments({
  1. required String threadId,
  2. String order = 'created_at.desc',
})

Lists approved comments for a given thread.

The returned collection is ordered according to order. By default the backend sorts by created_at descending. Pass a PostgREST-style order string (e.g. "created_at.asc") to change the sorting. Throws CommentsApiException when the request fails or the response cannot be parsed.

Implementation

Future<List<Comment>> listComments({
  required String threadId,
  String order = 'created_at.desc',
}) async {
  final token = await _ensureToken();
  final uri = _uri('/api/comments', {
    'thread_id': threadId,
    'order': order,
  });

  final response = await _http.get(uri, headers: {
    'Authorization': token.token,
  });

  if (response.statusCode >= 400) {
    throw _mapError(response);
  }

  final decoded = jsonDecode(response.body);
  if (decoded is! List) {
    throw CommentsApiException(
      response.statusCode,
      'invalid_response',
      details: decoded,
    );
  }

  return decoded
      .whereType<Map<String, dynamic>>()
      .map(Comment.fromJson)
      .toList(growable: false);
}