joinGroup static method

Future<Group?> joinGroup(
  1. String guid,
  2. String groupType, {
  3. String password = '',
  4. required dynamic onSuccess(
    1. Group group
    )?,
  5. required dynamic onError(
    1. CometChatException excep
    )?,
})

Joins an existing group.

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

Android Reference: GroupsRequest.joinGroup(String guid, String groupType, String password)

Returns a Group object after joining the logged in user to specified group. In order to start participating in group conversations, you will have to join a group

guid A unique identifier for a group that logged in user wants to join.

groupType needs to be either of the below 3 values public, private, password.

password Mandatory in case group type is password only.

method could throw PlatformException with error codes specifying the cause

Implementation

static Future<Group?> joinGroup(String guid, String groupType,
    {String password = '',
    required Function(Group group)? onSuccess,
    required Function(CometChatException excep)? onError}) async {
  try {
    // Get SDK instance
    final sdk = SdkRegistry.getInstance();

    // Call repository (pass null if password is empty)
    final joinedGroup = await sdk.groups.joinGroup(
      guid,
      groupType,
      password: password.isEmpty ? null : password,
    );

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