initiateCall static method

  1. @Deprecated('Use CometChatCalls.initiateCall (cometchat_calls_sdk) instead')
void initiateCall(
  1. Call call, {
  2. required dynamic onSuccess(
    1. Call call
    )?,
  3. required dynamic onError(
    1. CometChatException excep
    ),
})

Initiates a call to a user or group.

Android Reference: CometChat.initiateCall(Call call, CallbackListener<Call>)

Response parsing uses Call.fromMap() to match Android SDK's Call.fromJson():

  • sessionId from data.entities.on.entity.sessionid
  • callInitiator from data.entities.on.entity.data.entities.sender
  • callReceiver from data.entities.on.entity.data.entities.receiver
  • sender from data.entities.by.entity
  • receiver from data.entities.for.entity

Implementation

@Deprecated(
    'Use CometChatCalls.initiateCall (cometchat_calls_sdk) instead')
static void initiateCall(
  Call call, {
  required Function(Call call)? onSuccess,
  required Function(CometChatException excep) onError,
}) async {
  try {
    // Validate parameters
    if (call.receiverUid.isEmpty) {
      onError(
        CometChatException(
          "Error",
          "Receiver UID is empty",
          "Receiver UID is required",
        ),
      );
      return;
    }
    if (call.receiverType.isEmpty) {
      onError(
        CometChatException(
          "Error",
          "Receiver type is empty",
          "Receiver type is required",
        ),
      );
      return;
    }
    if (call.type.isEmpty) {
      onError(
        CometChatException(
          "Error",
          "Call type is empty",
          "Call type is required",
        ),
      );
      return;
    }

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

    // Call native Dart call repository - returns raw response map
    final responseData = await sdk.calls.initiateCallRaw(
      receiver: call.receiverUid,
      receiverType: call.receiverType,
      type: call.type,
    );

    // Parse using Call.fromMap() to match Android SDK's Call.fromJson()
    // Android: Call.fromJson(mainObject.getJSONObject("data").toString())
    final publicCall = Call.fromMap(responseData);

    // Android: CallManager.initiateCall() stores this as the initial call.
    _initialCall = publicCall;

    // 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(),
      ),
    );
  }
}