endCall static method

void endCall(
  1. String sessionID, {
  2. int? joinedAt,
  3. required dynamic onSuccess(
    1. Call call
    )?,
  4. required dynamic onError(
    1. CometChatException excep
    ),
})

Ends an ongoing call.

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

joinedAt is the epoch-seconds timestamp at which the current user joined the session. PUT /calls/:sessionId rejects a status=ended update without it ("The joinedAt post parameter is required to end a call"), which left the call "ongoing" server-side so the other participant never received the call-ended event. When omitted it is resolved from the call cached by initiateCall / acceptCall, matching how Android reads CallManager.initialCall.

Android Reference: CometChat.endCall(String sessionID, CallbackListener<Call>)

Implementation

static void endCall(String sessionID,
    {int? joinedAt,
    required Function(Call call)? onSuccess,
    required Function(CometChatException excep) onError}) async {
  try {
    // Validate parameters
    if (sessionID.isEmpty) {
      onError(CometChatException(
          "Error", "Session ID is null", "Session ID is null"));
      return;
    }

    // Get SDK instance
    final sdk = SdkRegistry.getInstance();

    // Android: endCallInternal() reads joinedAt off the cached initial call
    // and always passes it to updateCallStatus().
    final effectiveJoinedAt = joinedAt ?? joinedAtForSession(sessionID);

    // Call native Dart call repository - returns raw response map
    final responseData = await sdk.calls
        .updateCallStatusRaw(sessionID, 'ended', joinedAt: effectiveJoinedAt);

    // Parse using Call.fromMap() to match Android SDK's Call.fromJson()
    final publicCall = Call.fromMap(responseData);

    // Android: CallManager.endCall() tears down the cached call.
    _initialCall = null;

    // Call success callback
    if (onSuccess != null) onSuccess(publicCall);
  } on SdkException catch (sdkEx) {
    // Convert SdkException to CometChatException
    final cometChatEx = CometChatException(
      sdkEx.code,
      sdkEx.details ?? sdkEx.message,
      sdkEx.message,
    );
    onError(cometChatEx);
  } catch (e) {
    onError(CometChatException(
      ErrorCode.errorUnhandledException,
      e.toString(),
      e.toString(),
    ));
  }
}