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

# Create A Group

> Create public, private, or password-protected groups and optionally add members during creation using the CometChat Flutter SDK.

<Accordion title="AI Integration Quick Reference">
  ```dart theme={null}
  // Create a group
  Group group = Group(guid: "GUID", name: "Group Name", type: CometChatGroupType.public);
  await CometChat.createGroup(
    group: group,
    onSuccess: (Group group) => debugPrint("Created: ${group.name}"),
    onError: (CometChatException e) => debugPrint("Error: ${e.message}"),
  );

  // Create group with members
  CometChat.createGroupWithMembers(
    group: group,
    groupMembers: [GroupMember(uid: "UID", scope: GroupMemberScope.participant)],
    bannedUserIds: [],
    onSuccess: (Group group) => debugPrint("Created with members: ${group.name}"),
    onError: (CometChatException e) => debugPrint("Error: ${e.message}"),
  );
  ```

  **Group types:** `CometChatGroupType.public` | `CometChatGroupType.password` | `CometChatGroupType.private`
  **Member scopes:** `GroupMemberScope.admin` | `GroupMemberScope.moderator` | `GroupMemberScope.participant`
</Accordion>

Create groups for multi-user conversations. You can create a group on its own with `createGroup()`, or create one and add members in a single call with `createGroupWithMembers()`. See the [Group Class](#group-class) reference at the bottom for all available fields.

## Create a Group

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

<Note>
  **Available via:** SDK | [REST API](https://api-explorer.cometchat.com) | [UI Kits](/ui-kit/flutter/overview)
</Note>

Use `createGroup()` to create a new group. Pass a `Group` object with the group details.

| Group Type | Constant                      | Description                               |
| ---------- | ----------------------------- | ----------------------------------------- |
| Public     | `CometChatGroupType.public`   | Any user can join                         |
| Password   | `CometChatGroupType.password` | Users must provide the correct password   |
| Private    | `CometChatGroupType.private`  | Users must be added by an admin/moderator |

### Parameters

| Parameter   | Type                                  | Description                                                                            |
| ----------- | ------------------------------------- | -------------------------------------------------------------------------------------- |
| `group`     | `Group`                               | An instance of the `Group` class containing group details (guid, name, type, password) |
| `onSuccess` | `Function(Group group)?`              | Callback triggered on successful group creation                                        |
| `onError`   | `Function(CometChatException excep)?` | Callback triggered on error                                                            |

<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>

<Accordion title="Response">
  **On Success** — A `Group` object containing all details of the newly created group:

  <span id="create-group-group-object" style={{scrollMarginTop: '100px'}} />

  **Group Object:**

  | Parameter           | Type    | Description                                              | Sample Value         |
  | ------------------- | ------- | -------------------------------------------------------- | -------------------- |
  | `guid`              | string  | Unique identifier for the group                          | `"cometchat-guid-1"` |
  | `name`              | string  | Display name of the group                                | `"Hello Group!"`     |
  | `icon`              | string  | URL of the group icon                                    | `null`               |
  | `description`       | string  | Description of the group                                 | `null`               |
  | `membersCount`      | number  | Number of members in the group                           | `1`                  |
  | `metadata`          | object  | Custom metadata attached to the group                    | `{}`                 |
  | `joinedAt`          | number  | Epoch timestamp when the logged-in user joined the group | `1745554729`         |
  | `hasJoined`         | boolean | Whether the logged-in user has joined the group          | `true`               |
  | `createdAt`         | number  | Epoch timestamp when the group was created               | `1745554729`         |
  | `owner`             | string  | UID of the group owner                                   | `"cometchat-uid-1"`  |
  | `updatedAt`         | number  | Epoch timestamp when the group was last updated          | `1745554729`         |
  | `tags`              | array   | List of tags associated with the group                   | `[]`                 |
  | `type`              | string  | Type of the group (public, private, password)            | `"public"`           |
  | `scope`             | string  | Scope of the logged-in user in the group                 | `"admin"`            |
  | `password`          | string  | Password for password-protected groups                   | `null`               |
  | `isBannedFromGroup` | boolean | Whether the logged-in user is banned from the group      | `false`              |
</Accordion>

<Accordion title="Error">
  | Parameter | Type   | Description                  | Sample Value                                   |
  | --------- | ------ | ---------------------------- | ---------------------------------------------- |
  | `code`    | string | Error code identifier        | `"ERR_GUID_NOT_FOUND"`                         |
  | `message` | string | Human-readable error message | `"The specified group does not exist."`        |
  | `details` | string | Additional technical details | `"Please provide a valid GUID for the group."` |
</Accordion>

On success, returns a [`Group`](/sdk/reference/entities#group) object with the created group's details.

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

## Add Members While Creating a Group

Use `createGroupWithMembers()` to create a group and add members in one operation.

### Parameters

| Parameter       | Type                                  | Description                                                        |
| --------------- | ------------------------------------- | ------------------------------------------------------------------ |
| `group`         | `Group`                               | The `Group` object with group details (guid, name, type, password) |
| `groupMembers`  | `List<GroupMember>`                   | List of `GroupMember` objects to add during creation               |
| `bannedUserIds` | `List<String>`                        | List of UIDs to ban upon creation (defaults to empty)              |
| `onSuccess`     | `Function(Group group)?`              | Callback triggered on successful creation                          |
| `onError`       | `Function(CometChatException excep)?` | Callback triggered on error                                        |

Create a `GroupMember` with: `GroupMember(uid: "UID", scope: GroupMemberScope.participant)`

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

    Group group = Group(guid: GUID, name: groupName, type: groupType);
    List<GroupMember> members = [
      GroupMember(uid: "cometchat-uid-1", scope: GroupMemberScope.participant),
    ];
    List<String> bannedUserIds = ["cometchat-uid-2"];

    CometChat.createGroupWithMembers(
      group: group,
      groupMembers: members,
      bannedUserIds: bannedUserIds,
      onSuccess: (Group group) {
        debugPrint("Group created with members: ${group.name}");
      },
      onError: (CometChatException e) {
        debugPrint("Error creating group with members: ${e.message}");
      },
    );
    ```
  </Tab>
</Tabs>

<Accordion title="Response">
  **On Success** — A `Group` object containing all details of the newly created group:

  <span id="create-group-with-members-group-object" style={{scrollMarginTop: '100px'}} />

  **Group Object:**

  | Parameter           | Type    | Description                                              | Sample Value          |
  | ------------------- | ------- | -------------------------------------------------------- | --------------------- |
  | `guid`              | string  | Unique identifier for the group                          | `"cometchat-guid-11"` |
  | `name`              | string  | Display name of the group                                | `"Hello Group!"`      |
  | `icon`              | string  | URL of the group icon                                    | `null`                |
  | `description`       | string  | Description of the group                                 | `null`                |
  | `membersCount`      | number  | Number of members in the group                           | `2`                   |
  | `metadata`          | object  | Custom metadata attached to the group                    | `{}`                  |
  | `joinedAt`          | number  | Epoch timestamp when the logged-in user joined the group | `1745554729`          |
  | `hasJoined`         | boolean | Whether the logged-in user has joined the group          | `true`                |
  | `createdAt`         | number  | Epoch timestamp when the group was created               | `1745554729`          |
  | `owner`             | string  | UID of the group owner                                   | `"cometchat-uid-1"`   |
  | `updatedAt`         | number  | Epoch timestamp when the group was last updated          | `1745554729`          |
  | `tags`              | array   | List of tags associated with the group                   | `[]`                  |
  | `type`              | string  | Type of the group (public, private, password)            | `"public"`            |
  | `scope`             | string  | Scope of the logged-in user in the group                 | `"admin"`             |
  | `password`          | string  | Password for password-protected groups                   | `null`                |
  | `isBannedFromGroup` | boolean | Whether the logged-in user is banned from the group      | `false`               |
</Accordion>

<Accordion title="Error">
  | Parameter | Type   | Description                  | Sample Value                                   |
  | --------- | ------ | ---------------------------- | ---------------------------------------------- |
  | `code`    | string | Error code identifier        | `"ERR_GUID_NOT_FOUND"`                         |
  | `message` | string | Human-readable error message | `"The specified group does not exist."`        |
  | `details` | string | Additional technical details | `"Please provide a valid GUID for the group."` |
</Accordion>

## Group Class

The [`Group`](/sdk/reference/entities#group) object has the following fields. Fields marked "Yes" in the Editable column can be modified after creation using `updateGroup()`.

| 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.                               |

***

## Next Steps

<CardGroup cols={2}>
  <Card title="Join a Group" icon="right-to-bracket" href="/sdk/flutter/join-group">
    Join public, private, or password-protected groups
  </Card>

  <Card title="Add Members" icon="user-plus" href="/sdk/flutter/group-add-members">
    Add users to an existing group
  </Card>

  <Card title="Retrieve Groups" icon="list" href="/sdk/flutter/retrieve-groups">
    Fetch and filter group lists
  </Card>

  <Card title="Groups Overview" icon="users" href="/sdk/flutter/groups-overview">
    Overview of all group management features
  </Card>
</CardGroup>
