> ## Documentation Index
> Fetch the complete documentation index at: https://social-b97141fb-feat-user-history-moderation-detail.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Flutter Live Objects & Collections

> Observe social.plus Flutter SDK objects and collections with Dart streams, LiveCollection, and LiveResult.

The Flutter SDK exposes live objects as Dart streams and live collections through `LiveCollection` / `LiveCollectionStream`. A live collection emits `LiveResult<T>`, which contains the current `data` list and an `isFetching` flag.

## Platform Surface

| Surface | Public shape | Notes |
| - | - | - |
| Live object | `Stream<T>` | Example: `AmitySocialClient.newPostRepository().live.getPost(postId)`. |
| Live collection | `LiveCollection<T>` | Returned by builders such as `getPosts().targetCommunity(...).getLiveCollection()`. |
| Collection stream | `Stream<LiveResult<T>>` | Access with `liveCollection.getStream()`. |
| Loading state | `LiveResult<T>.isFetching` or `observeLoadingState()` | Use this to drive loading indicators. |
| Pagination | `loadNext()`, `loadPrevious()`, `hasNextPage()`, `hasPreviousPage()`, `reset()` | `getStream()` also triggers the first load. |
| Cleanup | `StreamSubscription.cancel()` and `liveCollection.dispose()` | Cancel listeners and dispose the live collection when the widget or bloc is done. |

## Parameters

| Parameter | Used by | Platforms | Description |
| - | - | - | - |
| `postId` or another entity ID | Live object streams | Flutter | ID of the object to observe. |
| Query builder target | Live collections | Flutter | Scope for the collection, such as `targetCommunity(communityId)` or `targetUser(userId)`. |
| `pageSize` | Live collections | Flutter | Optional page size for `getLiveCollection(pageSize: ...)`. Defaults to 20 when omitted. |
| `LiveResult<T>.data` | Collection stream | Flutter | Current list snapshot. |
| `LiveResult<T>.isFetching` | Collection stream | Flutter | Whether the collection is currently fetching. |

## Observe A Live Object

Observe a live object when your Flutter UI needs one SDK object to stay current after server or local-cache changes.

<CodeGroup>
  ```dart Flutter theme={null}
  final subscription = AmitySocialClient.newPostRepository()
      .live
      .getPost(postId)
      .listen((post) {
    final currentPostId = post.postId;
    showError(currentPostId ?? postId);
  });

  await subscription.cancel();
  ```
</CodeGroup>

## Observe A Live Collection

`getStream()` emits `LiveResult<AmityPost>`. Keep the collection instance so you can load more pages and dispose it later.

<CodeGroup>
  ```dart Flutter theme={null}
  final liveCollection = AmitySocialClient.newPostRepository()
      .getPosts()
      .targetCommunity(communityId)
      .getLiveCollection(pageSize: 20);

  liveCollection.onError((error, stackTrace) {
    showError(error ?? stackTrace);
  });

  final subscription = liveCollection.getStream().listen((result) {
    final posts = result.data;
    final isFetching = result.isFetching;

    showError('${posts.length}:$isFetching');
  });

  if (liveCollection.hasNextPage()) {
    await liveCollection.loadNext();
  }

  await subscription.cancel();
  await liveCollection.dispose();
  ```
</CodeGroup>

## Notes

* `getStream()` starts the first page load for `LiveCollection`.
* Use `observeLoadingState()` when you only need loading changes.
* Call `loadNext()` only when `hasNextPage()` is true.
* Call `reset()` to clear the collection and reload the first page.
* Cancel every `StreamSubscription` and call `dispose()` on live collections that are no longer used.

## Related Topics

<CardGroup cols={2}>
  <Card title="Post Retrieval" icon="newspaper" href="/social-plus-sdk/social/content-management/posts/retrieval/get-post">
    See post-specific Flutter retrieval examples.
  </Card>

  <Card title="Flutter Push Notifications" icon="bell" href="/social-plus-sdk/core-concepts/realtime-communication/push-notifications/setup/flutter-setup">
    Configure Flutter notification setup alongside real-time data.
  </Card>
</CardGroup>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.