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

# Add Members To A Group

> Add CometChat users to Flutter group chats with member scopes and optional banned member handling.

<Accordion title="AI Integration Quick Reference">
  | Field              | Value                                                                                                                                                                                                                                                       |
  | ------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
  | Package            | `cometchat_sdk`                                                                                                                                                                                                                                             |
  | Import             | `import 'package:cometchat_sdk/cometchat_sdk.dart';`                                                                                                                                                                                                        |
  | Purpose            | Add CometChat users to Flutter group chats with member scopes and optional banned member handling.                                                                                                                                                          |
  | Key methods        | `addGroupListener()` · `addMembersToGroup()`                                                                                                                                                                                                                |
  | Key classes        | `GroupMember` · `Group` · `User` · `CometChatMemberScope` · `Action` · `CometChatException`                                                                                                                                                                 |
  | Listener callbacks | `onMemberAddedToGroup()`                                                                                                                                                                                                                                    |
  | 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            | [Retrieve Group Members](/sdk/flutter/retrieve-group-members) · [Change Member Scope](/sdk/flutter/group-change-member-scope) · [Ban/Kick Member From A Group](/sdk/flutter/group-kick-member)                                                              |
  | Full reference     | [`GroupMember`](/sdk/reference/entities#groupmember) · [`Group`](/sdk/reference/entities#group) · [`User`](/sdk/reference/entities#user) · [`Action`](/sdk/reference/messages#action) · [`CometChatException`](/sdk/reference/auxiliary#cometchatexception) |
</Accordion>

## Add Members to Group

You can add members to the group using the `addMembersToGroup()` method. This method takes the below parameters:

1. `GUID` - GUID of the group users are to be added to.
2. `List<GroupMember> members` - This is a list of `GroupMember` objects. In order to add members, you need to create an object of the `GroupMember` class. The `UID` and the scope of the `GroupMember` are mandatory.
3. `List<String> bannedMembers` - This is the list of `UIDs` that need to be banned from the group. This can be set to `null` if there are no members to be banned.

<Tabs>
  <Tab title="Dart">
    ```dart theme={null}
    List<GroupMember> groupMembers = [];
    GroupMember firstMember = GroupMember.fromUid(
    scope: CometChatMemberScope.participant,
    uid: "cometchat-uid-3",name: "name");
    GroupMember secondMember = GroupMember.fromUid(scope: CometChatMemberScope.admin,
    uid: "cometchat-uid-4",name: "name");

    groupMembers = [firstMember, secondMember];

    CometChat.addMembersToGroup( guid: conversationWith,
            groupMembers: groupMembers,
            onSuccess: (Map<String?,String?> result) {
              debugPrint("Group Member added Successfully : $result");
            },
            onError: (CometChatException e) {
              debugPrint("Group Member addition failed with exception: ${e.message}");
            });
    ```
  </Tab>
</Tabs>

In the `onSuccess()` method , you will receive a Map which will contain the `UID` of the users and the value will either be `success` or an error message describing why the operation to add the user to the group or ban the user failed.

## Real-Time Group Member Added Events

*In other words, as a member of a group, how do I know when someone is added to the group when my app is running?*

<Note>
  When a group member is added by another member, this event is triggered. When a user joins a group on their own, the joined event is triggered.
</Note>

To receive real-time events whenever a new member is added to a group, you need to implement the `onMemberAddedToGroup()` methods of the `GroupListener` class.

* `onMemberAddedToGroup()` - This method is triggered when any user is added to the group so that the logged-in user is informed of the other members added to the group.

<Tabs>
  <Tab title="Dart">
    ```dart theme={null}
    class Class_Name  with GroupListener {

    //CometChat.addGroupListener("group_Listener_id", this);
    @override
    void onMemberAddedToGroup(Action action, User addedby, User userAdded, Group addedTo) {
      print("onMemberAddedToGroup");
    }
    }
    ```
  </Tab>
</Tabs>

## Member Added to Group event in Message History

*In other words, as a member of a group, how do I know when someone is added to the group when my app is not running?*

When you retrieve the list of previous messages if a member has been added to any group that the logged-in user is a member of, the list of messages will contain an `Action` message. An `Action` message is a sub-class of `BaseMessage` class.

For the group member added event, in the `Action` object received, the following fields can help you get the relevant information-

1. `action` - `added`
2. `actionOn` - User object containing the details of the user who was added to the group
3. `actionBy` - User object containing the details of the user who added the member to the group
4. `actionFor` - Group object containing the details of the group to which the member was added
