toList method

List toList()

Returns a list of all Comments in the CommentForest.

The resulting List of Comment objects is built in a breadth-first manner. For example, the CommentForest:

1

  • 2
    • 3
  • 4 5

Will return the comments in the following order: 1, 5, 2, 4, 3.

Implementation

List toList() {
  final comments = [];
  final queue = Queue.from(_comments);
  while (queue.isNotEmpty) {
    final comment = queue.removeFirst();
    comments.add(comment);
    if ((comment is! MoreComments) && (comment.replies != null)) {
      queue.addAll(comment.replies._comments);
    }
  }
  return comments;
}