Chat class

Main entry point for Chat SDK.

This is the facade class that provides all chat functionality. It follows a backend-agnostic pattern with offline-first support.

Features

  • Backend-agnostic (use any backend via adapters)
  • Offline-first with outbound queue
  • Real-time updates via event bus
  • Reactive streams for UI
  • Early initialization support (init before user login)

Example

// At app startup
final chat = await Chat.create(
  databasePath: 'chat.db',
  adapter: MyBackendAdapter(),
  identityProvider: MyIdentityProvider(),
  autoConnect: false, // Don't connect until user logs in
);

// After login, identityProvider emits the authenticated user id.
await chat.connect();

Constructors

Chat(ChatRegistry _registry)
Creates a new Chat instance.

Properties

connectionState ValueNotifier<ChatConnectionState>
Connection state notifier.
no setter
currentUserId String?
The current user ID.
no setter
hashCode int
The hash code for this object.
no setterinherited
isInitialized bool
Whether the SDK is initialized.
no setter
onEvent Stream<ChatEvent>
Stream of chat events (messages, typing, presence, etc.).
no setter
pendingCount ValueNotifier<int>
Pending operations count notifier.
no setter
registry ChatRegistry
The registry containing configuration and dependencies.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
sessionState ValueNotifier<ChatSessionState>
User-scoped readiness notifier.
no setter
syncStatus ValueNotifier<SyncStatus>
Sync status notifier.
no setter
totalUnreadCount Stream<int>
Stream of total unread count across all conversations.
no setter

Methods

addParticipants(String conversationId, List<String> userIds) Future<void>
Add participants to conversation.
addReaction(String messageId, String emoji) Future<void>
Add reaction to message.
archiveConversation(String conversationId) Future<void>
Archive a conversation (hide from main list but keep data).
clearSyncState() Future<void>
Clears the sync state.
connect() Future<void>
Connect to backend.
createConversation({required ConversationMode mode, ConversationType type = ConversationType.group, String? name, List<String>? participantIds, Duration? expiresIn, Map<String, dynamic>? extra}) Future<Conversation>
Create a new conversation.
deleteConversation(String conversationId) Future<void>
Delete/leave conversation.
deleteMessage(String conversationId, String messageId) Future<void>
Delete message.
disconnect() Future<void>
Disconnect from backend.
dispose() Future<void>
Dispose all resources.
getConversation(String conversationId) Future<Conversation?>
Get conversation by ID.
getConversationById(String conversationId) Future<Conversation?>
Get a conversation by ID (one-shot).
getConversations({ConversationFilter? filter}) Future<List<Conversation>>
Get all conversations (one-shot, not a stream).
getMessage(String messageId) Future<Message?>
Get a specific message by ID (one-shot).
getMessages(String conversationId, {int limit = 50}) Future<List<Message>>
Get messages for a conversation (one-shot, not a stream).
getPendingRequests(String conversationId) Future<List<Participant>>
Get pending join requests.
getPinnedEvents(String conversationId) Future<List<PinnedEvent>>
Get pinned events for conversation (one-shot).
getPresence(String userId) Future<PresenceResult>
Query the current presence status of a user.
getShareCode(String conversationId) Future<String>
Get share code for conversation.
getStarredMessages() Future<List<Message>>
Get starred messages (one-shot).
getStarredMessagesByConversation(String conversationId) Future<List<Message>>
Get starred messages by conversation (one-shot).
initialize() Future<void>
Initialize the chat SDK.
joinConversation({required String code, required String displayName, String? avatarUrl}) Future<Conversation>
Join conversation via code.
loadMoreMessages(String conversationId, {String? beforeId}) Future<LoadMessagesResult>
Load more messages (pagination).
markAsRead(String conversationId, String messageId) Future<void>
Mark messages as read.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
on<T extends ChatEvent>() Stream<T>
Stream of specific event type.
pinMessage(String conversationId, String messageId, {Duration? duration}) Future<void>
Pin message.
removeParticipant(String conversationId, String userId) Future<void>
Remove participant from conversation.
removeReaction(String messageId, String reactionIdOrEmoji) Future<void>
Remove reaction from message.
sendMessage({required String conversationId, required String content, MessageType type = MessageType.text, String? replyToId}) Future<Message>
Send a message.
sendMessageWithParams(SendMessageParams params) Future<Message>
Send a message with full params (including attachments).
sendTyping(String conversationId, {bool isTyping = true}) Future<void>
Send typing indicator.
starMessage(String conversationId, String messageId, {bool star = true}) Future<String>
Star/unstar message. Returns the star ID when starring, empty string when unstarring.
startHeartbeat() → void
Start the presence heartbeat.
stopHeartbeat() → void
Stop the presence heartbeat.
subscribePresence(String userId) Future<void>
Subscribe to presence updates for a user.
sync() Future<void>
Force sync.
syncConversation(String conversationId) Future<void>
Sync specific conversation.
toString() String
A string representation of this object.
inherited
unarchiveConversation(String conversationId) Future<void>
Unarchive a conversation (restore to main list).
unpinMessage(String conversationId, String messageId) Future<void>
Unpin message.
unstarMessage(String starId) Future<void>
Unstar message using the star record ID.
unsubscribePresence(String userId) Future<void>
Unsubscribe from presence updates for a user.
updateParticipantStatus(String conversationId, String userId, ParticipantStatus status) Future<void>
Update participant status (approve/reject).
watchConversations({ConversationFilter? filter}) Stream<List<Conversation>>
Watch all conversations.
watchMessages(String conversationId) Stream<List<Message>>
Watch messages for a conversation.
watchPinnedEvents(String conversationId) Stream<List<PinnedEvent>>
Watch pinned events for conversation (for displaying pin notifications in stream).
watchPinnedMessages(String conversationId) Stream<List<Message>>
Watch pinned messages for conversation.
watchStarredMessages() Stream<List<Message>>
Watch starred messages.

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

create({required String databasePath, required ChatAdapter adapter, required ChatIdentityProvider identityProvider, DatabaseEncryptionConfig? encryptionConfig, bool? autoConnect}) Future<Chat>
Creates, initializes, and optionally connects a Chat instance.