getConversation static method

Future<Conversation?> getConversation(
  1. String conversationWith,
  2. String conversationType, {
  3. required dynamic onSuccess(
    1. Conversation conversation
    )?,
  4. required dynamic onError(
    1. CometChatException excep
    )?,
  5. ConversationDetailsListener? listener,
})

In order to fetch a specific conversation

conversationWith : UID/GUID of the user/group whose conversation you want to fetch.

conversationType : user or group.

Migration Note: Migrated from platform channels to native Dart implementation. Behavior and signature remain identical for backward compatibility.

Android Reference: ConversationsRequest.getUserConversation() or ConversationsRequest.getGroupConversation()

The method could throw PlatformException with error codes specifying the cause

Implementation

static Future<Conversation?> getConversation(
  String conversationWith,
  String conversationType, {
  required Function(Conversation conversation)? onSuccess,
  required Function(CometChatException excep)? onError,
  ConversationDetailsListener? listener,
}) async {
  try {
    // Get SDK instance
    final sdk = SdkRegistry.getInstance();

    // Offline Read Coverage §2 — phase 1: the cached object, delivered
    // instantly and listener-only.
    //
    // ADDITIVE: the cached phase never settles `onSuccess` / `onError`, so a
    // caller who passes no listener sees the exact pre-offline contract —
    // one server delivery, unchanged.
    if (listener != null) {
      await _deliverCachedConversation(
        conversationWith,
        conversationType,
        listener,
      );
    }

    // Captured as the request goes out: an answer arriving after this
    // session ended is never written (BEH-SYNC-06).
    final issuedIn = OfflineRuntime.instance.readSession;

    // Call appropriate repository method based on conversation type
    final conversation = conversationType == 'user'
        ? await sdk.conversations.getUserConversation(conversationWith)
        : await sdk.conversations.getGroupConversation(conversationWith);

    // C-03 / BEH-READ-28: write the fetched conversation through to the cache
    // before delivering — insert-if-absent, LWW by `updatedAt`, keyed by the
    // canonical conversationId. No-op when offline is disabled / not logged in
    // or the session the request was issued in has ended: nothing is written.
    // Stays SINGLE-delivery — not a two-phase read.
    try {
      await OfflineRuntime.instance.writeIn(
        issuedIn,
        (store) => ConversationCacheWriter.writeThrough(store, conversation),
      );
    } catch (_) {
      /* best-effort — never break the point lookup */
    }

    // Phase 2 — the server object.
    listener?.onServerConversation(conversation);

    // Call success callback
    if (onSuccess != null) onSuccess(conversation);
    return conversation;
  } on SdkException catch (sdkEx) {
    // Convert SdkException to CometChatException
    final cometChatEx = CometChatException(
      sdkEx.code,
      sdkEx.details ?? sdkEx.message,
      sdkEx.message,
    );
    listener?.onError(cometChatEx);
    _errorCallbackHandler(cometChatEx, null, null, onError);
  } catch (e) {
    _errorCallbackHandler(null, null, e, onError);
  }
  return null;
}