> ## Documentation Index
> Fetch the complete documentation index at: https://cometchat-22654f5b-docs-llms-scoped-indexes.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Create A Group

> Create CometChat public, private, and password-protected groups in Flutter apps with group GUID, name, type, and password.

<Accordion title="AI Integration Quick Reference">
  | Field | Value |
  | - | - |
  | Package | `cometchat_sdk` |
  | Import | `import 'package:cometchat_sdk/cometchat_sdk.dart';` |
  | Purpose | Create CometChat public, private, and password-protected groups in Flutter apps with group GUID, name, type, and password. |
  | Key methods | `createGroup()` · `createGroupWithMembers()` |
  | Key classes | `Group` · `GroupMember` · `CometChatMemberScope` · `CometChatException` · `CometChatGroupType` |
  | Prerequisites | SDK initialised via [`CometChat.init()`](/sdk/flutter/setup) and a logged-in user via [`CometChat.login()`](/sdk/flutter/authentication-overview). |
  | Constraints | `onSuccess` and `onError` are **both required** — omitting either is a compile error, and awaiting the call alone gives you nothing to act on. Put the success path inside `onSuccess`. |
  | Related | [Join A Group](/sdk/flutter/join-group) · [Update A Group](/sdk/flutter/update-group) · [Delete A Group](/sdk/flutter/delete-group) · [Add Members To A Group](/sdk/flutter/group-add-members) |
  | Full reference | [`Group`](/sdk/reference/entities#group) · [`GroupMember`](/sdk/reference/entities#groupmember) · [`CometChatException`](/sdk/reference/auxiliary#cometchatexception) |
</Accordion>

## Create a Group

*In other words, as a logged-in user, how do I create a public, private or password-protected group?*

You can create a group using `createGroup()` method. This method takes a `Group` object as input.

The `groupType` needs to be either of the below 3 values:

1.`CometChatGroupType.`*public* (public)

2.`CometChatGroupType.`*password* (password)

3.`CometChatGroupType.`*private* (private)

<Tabs>
  <Tab title="Dart">
    ```dart theme={null}
    String GUID = "GUID";
      String groupName = "Hello Group!";
      String groupType = CometChatGroupType.public;
      String password = "";

      Group _group = Group(guid: GUID, name: groupName, type: groupType);


      await CometChat.createGroup(group: _group, onSuccess: (Group group ){
        debugPrint("Group Created Successfully : $group ");
      }, onError:(CometChatException e) {
        debugPrint("Group Creation failed with exception: ${e.message}");
      } );
    ```
  </Tab>
</Tabs>

The `createGroup()` method takes the following parameters:

| Parameter | Description |
| - | - |
| `group` | An instance of `Group` class |

After the successful creation of the group, you will receive an instance of `Group` class which contains all the information about the particular group.

<Warning>
  GUID can be alphanumeric with underscore and hyphen. Spaces, punctuation and other special characters are not allowed.
</Warning>

## Create a Group with Members

*In other words, how do I create a group and add members in a single step?*

You can create a group and add members simultaneously using the `createGroupWithMembers()` method. This is more efficient than creating a group first and then adding members separately.

<Tabs>
  <Tab title="Dart">
    ```dart theme={null}
    String GUID = "GUID";
    String groupName = "Hello Group!";
    String groupType = CometChatGroupType.public;

    Group group = Group(guid: GUID, name: groupName, type: groupType);

    // Create a list of group members to add
    List<GroupMember> members = [];
    members.add(GroupMember(uid: "cometchat-uid-1", scope: CometChatMemberScope.participant));
    members.add(GroupMember(uid: "cometchat-uid-2", scope: CometChatMemberScope.moderator));
    members.add(GroupMember(uid: "cometchat-uid-3", scope: CometChatMemberScope.admin));

    await CometChat.createGroupWithMembers(
      group: group,
      members: members,
      onSuccess: (Group group, Map<String, String> failedMembers) {
        debugPrint("Group Created Successfully : $group");
        if (failedMembers.isNotEmpty) {
          debugPrint("Failed to add some members: $failedMembers");
        }
      },
      onError: (CometChatException e) {
        debugPrint("Group Creation failed with exception: ${e.message}");
      }
    );
    ```
  </Tab>
</Tabs>

The `createGroupWithMembers()` method takes the following parameters:

| Parameter | Description |
| - | - |
| `group` | An instance of `Group` class |
| `members` | A list of `GroupMember` objects to be added to the group |

The `onSuccess` callback returns:

* `group`: The created `Group` object
* `failedMembers`: A map containing UIDs of members that failed to be added along with the error reason

The `GroupMember` class takes the following parameters:

| Parameter | Description |
| - | - |
| `uid` | The UID of the user to be added as a member |
| `scope` | The scope of the member: `CometChatMemberScope.participant`, `moderator`, or `admin` |

## Group Class

| Field | Editable | Information |
| - | - | - |
| guid | Needs to be specified at group creation. Cannot be edited later | A unique identifier for a group |
| name | Yes | Name of the group |
| type | No | Type of the group: Can be 1. Public 2. Password 3. Private |
| password | No | Password for the group in case the group is of type password. |
| icon | Yes | An URL to group icon |
| description | Yes | Description about the group |
| owner | Yes | UID of the owner of the group. |
| metadata | Yes | Additional data for the group as JSON |
| createdAt | No | The unix timestamp of the time the group was created |
| updatedAt | No | The unix timestamp of the time the group was last updated |
| hasJoined | No | A boolean to determine if the logged in user is a member of the group. |
| joinedAt | No | The unix timestamp of the time the logged in user joined the group. |
| scope | Yes | Scope of the logged in user. Can be: 1. Admin 2. Moderator 3. Participant |
| membersCount | No | The number of members in the groups |
| tags | Yes | A list of tags to identify specific groups. |
| isBannedFromGroup | No | A boolean indicating whether the logged-in user is banned from the group. |


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.