fetchComments method

Future<void> fetchComments(
  1. String postId
)

Implementation

Future<void> fetchComments(String postId) async {
  final token = Hive.box('user_data').get('user_token');

  try {
    isLoading.value = true;
    final response = await _apiService.get(
      endpoint: CommunityConstants.comments,
      query: {'post_id': postId},
      headers: {
        'api-key': CommunityConstants.apiKey,
        if (token != null) 'Authorization': 'Bearer $token',
        'Accept': 'application/json',
      },
    );

    if (response.statusCode == 200) {
      final data = response.data is String
          ? jsonDecode(response.data)
          : response.data;
      comments.value = CommentsResponse.fromJson(data);
    } else {
      Get.snackbar('Error', 'Failed to load comments');
    }
  } catch (e) {
    Get.snackbar('Error', 'Failed to load comments: ${e.toString()}');
  } finally {
    isLoading.value = false;
  }
}