> ## Documentation Index
> Fetch the complete documentation index at: https://learn.social.plus/llms.txt
> Use this file to discover all available pages before exploring further.

# Get Latest Comment

> Fetch the newest comment for a post or content item, with platform-specific helper availability.

Use the latest-comment pattern when you need a compact preview, such as showing the newest comment below a post card. iOS and Android expose dedicated latest-comment helpers. TypeScript and Flutter use the normal comment query API sorted newest-first with a page size of one.

## Platform Availability

| Platform   | Dedicated latest helper                                                                               | Reference targets          |
| ---------- | ----------------------------------------------------------------------------------------------------- | -------------------------- |
| iOS        | `getLatestComment(withReferenceId:referenceType:includeReplies:)`                                     | `post`, `content`, `story` |
| Android    | `getLatestComment().post(...)` / `.content(...)`                                                      | `post`, `content`          |
| TypeScript | Use `getComments(...)` with `sortBy: "lastCreated"` and `pageSize: 1`                                 | `post`, `content`, `story` |
| Flutter    | Use `getComments().post(...)`, `.content(...)`, or `.story(...)` with newest-first sort and limit `1` | `post`, `content`, `story` |

## Parameters

| Operation                | Parameter            | Required | Description                                                               |
| ------------------------ | -------------------- | -------- | ------------------------------------------------------------------------- |
| Dedicated helpers        | `referenceId`        | Yes      | Target content ID whose latest comment should be fetched.                 |
| Dedicated helpers        | `referenceType`      | Yes      | Target content type, such as `post`.                                      |
| Dedicated helpers        | `includeReplies`     | No       | Whether replies can be returned as the latest comment on iOS and Android. |
| Query the latest comment | `sortBy`             | Yes      | Newest-first sort order, such as `lastCreated`.                           |
| Query the latest comment | `pageSize` / `limit` | Yes      | Set to `1` to retrieve only the newest comment.                           |

## Dedicated Helpers

Use the dedicated iOS and Android helpers when you want the latest comment without building a manual one-item query.

<CodeGroup>
  ```swift iOS theme={null}
  let latestComment = try await commentRepository.getLatestComment(
      withReferenceId: "post-id",
      referenceType: .post,
      includeReplies: true
  )

  showSuccessMessage(latestComment.commentId)
  ```

  ```kotlin Android theme={null}
  AmitySocialClient.newCommentRepository()
      .getLatestComment()
      .post(postId = postId)
      .includeReplies(includeReplies = true)
      .build()
      .query()
      .subscribe(
          { comment -> showSuccessMessage(comment.getCommentId()) },
          { error -> handleGeneralError(error) }
      )
  ```
</CodeGroup>

## Query the Latest Comment

Use this pattern on TypeScript and Flutter, or when you want the same query-based behavior across platforms.

<CodeGroup>
  ```typescript TypeScript theme={null}
  import { CommentRepository } from "@amityco/ts-sdk";

  const unsubscribe = CommentRepository.getComments(
    {
      referenceType: "post",
      referenceId: postId,
      sortBy: "lastCreated",
      pageSize: 1,
    },
    ({ data: comments, loading, error }) => {
      if (loading) return;

      if (error) {
        handleError(error);
        return;
      }

      const latestComment = comments?.[0];
      if (latestComment) {
        renderResults(latestComment);
      }
    },
  );
  ```

  ```dart Flutter theme={null}
  final comments = await AmitySocialClient.newCommentRepository()
      .getComments()
      .post(postId)
      .sortBy(AmityCommentSortOption.LAST_CREATED)
      .includeDeleted(false)
      .query(limit: 1);

  final latestComment = comments.isNotEmpty ? comments.first : null;
  ```
</CodeGroup>

## Include Replies

For iOS and Android dedicated helpers, `includeReplies` controls whether replies can be returned as the latest comment.

| Value   | Result                                          |
| ------- | ----------------------------------------------- |
| `true`  | The newest comment at any level can be returned |
| `false` | Only top-level comments are considered          |

For query-based implementations, use the `parentId` filter:

* Omit `parentId` to query comments from all levels where the SDK supports it.
* Set `parentId` to `null` / `nil` to query only top-level comments.
* Set `parentId` to a comment ID to query replies to that comment.

## Notes

* The dedicated iOS and Android latest-comment helpers return one comment, not a live object or live collection.
* Use [Query Comments](/social-plus-sdk/social/content-management/comments/retrieval/query-comments) when you need pagination, filtering, or a full thread.
* Handle the empty state in query-based implementations because a reference may not have comments yet.

## Related Topics

<CardGroup cols={2}>
  <Card title="Query Comments" href="/social-plus-sdk/social/content-management/comments/retrieval/query-comments" icon="search">
    Query comments with parent filters, deleted-state filters, and pagination.
  </Card>

  <Card title="Get Comment" href="/social-plus-sdk/social/content-management/comments/retrieval/get-comment" icon="message">
    Retrieve a specific comment by ID.
  </Card>
</CardGroup>
