getCommentsForVideo method

Future<PagingController<int, Comment>?> getCommentsForVideo(
  1. String videoUrl
)

Returns the paging controller for a video's comments.

Implementation

Future<PagingController<int, Comment>?> getCommentsForVideo(
    String videoUrl) async {
  Video video = await getVideoFromUrl(videoUrl);
  PagingController<int, Comment>? pagingController =
      _commentsPagingCache[video.id.value];
  if (pagingController != null) {
    return pagingController;
  }

  CommentsList? commentsList = await _commentsClient.getComments(video);
  pagingController = PagingController(firstPageKey: 1);

  pagingController.addPageRequestListener((pageKey) async {
    List<Comment> comments = [];

    try {
      comments.addAll(commentsList!.toList());
      commentsList = await compute(computeCommentsList, commentsList);
    } finally {
      if (comments.isEmpty) {
        pagingController?.appendLastPage(comments);
      } else {
        pagingController?.appendPage(comments, pageKey + 1);
      }
    }
  });
  _commentsPagingCache[video.id.value] = pagingController;

  return pagingController;
}