createGroupWithMembers static method

Future<Group?> createGroupWithMembers({
  1. required Group group,
  2. required List<GroupMember> groupMembers,
  3. List<String> bannedUserIds = const [],
  4. dynamic onSuccess(
    1. Group group
    )?,
  5. dynamic onError(
    1. CometChatException excep
    )?,
})

Creates a group in CometChat along with initial members and returns the created Group.

This method is similar to createGroup but also accepts a list of GroupMember objects to be added during group creation. Additionally, you can pass a list of user IDs to ban immediately using bannedUserIds.

The group parameter should contain required fields like guid, name, and type. If the group type is "password", a password must also be provided.

groupMembers is a list of users to be added to the group with their respective scopes. bannedUserIds is an optional list of users to be banned upon creation.

The result is returned via onSuccess or onError callbacks, and the created Group object is returned on success.

Throws a PlatformException if a platform channel error occurs.

Example:

Creates a group with initial members and optionally bans users.

**Migration Note:** Migrated from platform channels to native Dart.
This is a convenience method that orchestrates multiple operations:
1. Creates the group
2. Adds members to the group
3. Bans specified users (if provided)
Behavior and signature remain identical.

**Android Reference:** `GroupsRequest.createGroup()` + `GroupMembersRequest.addMembers()` + `BannedMembersRequest.banMember()`

CometChat.createGroupWithMembers(
  group: Group(guid: "dev-group", name: "Dev Team", type: GroupType.private),
  groupMembers: [
    GroupMember(uid: "user1", scope: GroupMemberScope.admin),
    GroupMember(uid: "user2", scope: GroupMemberScope.participant),
  ],
  bannedUserIds: ["spammer123"],
  onSuccess: (group) => print("Group with members created: ${group.name}"),
  onError: (e) => print("Failed: ${e.message}")
);

Implementation

static Future<Group?> createGroupWithMembers({
  required Group group,
  required List<GroupMember> groupMembers,
  List<String> bannedUserIds = const [],
  Function(Group group)? onSuccess,
  Function(CometChatException excep)? onError,
}) async {
  try {
    // Get SDK instance
    final sdk = SdkRegistry.getInstance();

    // Step 1: Create the group
    final createdGroup = await sdk.groups.createGroup(group);

    // Step 2: Add members to the group (if any)
    if (groupMembers.isNotEmpty) {
      try {
        await sdk.groupMembers.addMembers(createdGroup.guid, groupMembers);
      } catch (e) {
        // Log error but continue - group was created successfully
        Logger.error('CometChat',
            'Failed to add members during group creation', e, null);
      }
    }

    // Step 3: Ban users (if any)
    if (bannedUserIds.isNotEmpty) {
      for (final uid in bannedUserIds) {
        try {
          await sdk.bannedMembers.banMember(createdGroup.guid, uid);
        } catch (e) {
          // Log error but continue - group was created successfully
          Logger.error('CometChat',
              'Failed to ban user $uid during group creation', e, null);
        }
      }
    }

    // Call success callback
    if (onSuccess != null) onSuccess(createdGroup);
    return createdGroup;
  } 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;
}