toggleLike method

Future<void> toggleLike(
  1. String commentId
)

Toggles the like status of a comment.

Updates the comment's like status and count in the local state. The comment must be in the comments list for the update to take effect.

commentId is the ID of the comment to toggle like for.

If the operation fails or the comment is not found, this method does nothing.

Implementation

Future<void> toggleLike(String commentId) async {
  final Comment? comment = await controller.toggleLike(commentId);
  if (comment == null) return;
  final index = _comments.indexWhere((c) => c.id == commentId);
  if (index != -1) {
    _comments[index] = comment.copyWith(replies: _comments[index].replies);
    notifyListeners();
  }
}