Invite members to private community (Flutter)

I am inviting members to private community like below method addFriendsInCommunity. In the received variable community. I do not see any memberCount update or members updated. What is the correct way to add friends to the community. Is there a different steps I have to take if community is private vs public?

  Future<AmityCommunity> addFriendsInCommunity({
    required String communityId,
    required List<String> userIds,
  }) async {
    try {
      final community = await AmitySocialClient.newCommunityRepository()
          .updateCommunity(communityId)
          .userIds(userIds)
          .update();

      AppLogger.logInfo("Friends Added in Community: ${community.communityId}");

      return community;
    } catch (exception) {
      AppLogger.logError("Friend Added in Community error = $exception");
      rethrow;
    }
  }


@danielkang98 Let me pass this to check with my team, i’ll back to you soon.

1 Like

Hello @danielkang98, From the issue you’re facing, I suggest using StreamBuilder as shown in the code snippet below:

 return StreamBuilder<AmityCommunity>(
          stream: widget.community.listen.stream,
          initialData: widget.community,
          builder: (context, snapshot) {
            var memberCount = snapshot.data!.membersCount!;

I am calling addFriendsInCommunity method to invite members into a private community but the membercount is not updated, can you share an example on how to update members in a private community? Instead of other users having to manually join? Like you have mentioned I have used StreamBuilder like below to see changes, but the member count is not being updated.

  Future<AmityCommunity> addFriendsInCommunity({
    required String communityId,
    required List<String> userIds,
  }) async {
    try {
      final community = await AmitySocialClient.newCommunityRepository()
          .updateCommunity(communityId)
          .userIds(userIds)
          .update();

      AppLogger.logInfo("Friends Added in Community: ${community.communityId}");

      return community;
    } catch (exception) {
      AppLogger.logError("Friend Added in Community error = $exception");
      rethrow;
    }
  }
Scaffold(
        appBar: AppBar(
          scrolledUnderElevation: 0,
          leading: BackButton(
            onPressed: () {
              Get.back(result: true);
            },
          ),
          title: Text(widget.community.displayName ?? "N/A"),
          backgroundColor: AppColors.primaryBackground,
          actions: [
            PopupMenuButton<String>(
              color: AppColors.secondaryBackground,
              icon: const Icon(Icons.more_vert),
              onSelected: (String result) {
                // Handle the action based on the selected value
                switch (result) {
                  case 'edit':
                    // Handle edit action
                    break;
                  case 'delete':
                    _deleteCommunity();
                    break;
                  // Add more cases as needed
                }
              },
              itemBuilder: (BuildContext context) => <PopupMenuEntry<String>>[
                const PopupMenuItem<String>(
                  value: 'edit',
                  child: Text('Edit Group'),
                ),
                const PopupMenuItem<String>(
                  value: 'delete',
                  child: Text('Delete Group'),
                ),
                // Add more items as needed
              ],
            ),
          ],
        ),
        backgroundColor: AppColors.secondaryBackground,
        body: StreamBuilder<AmityCommunity>(
          stream: widget.community.listen.stream,
          initialData: widget.community,
          builder: (context, snapshot) {
            var memberCount = snapshot.data!.membersCount!;
            return memberCount > 1
                ? SingleChildScrollView(
                    child: Column(
                      children: [
                        _buildHeader(),
                        _buildMemberSection(),
                        _buildMonthlyRecap(),
                        const SizedBox(height: 6),

                        // Add other sections here as needed
                      ],
                    ),
                  )
                : Column(
                    children: [
                      _buildHeader(),
                      _buildNoMembersInviteWidget(),
                    ],
                  );
          },
        ),
      ),

@danielkang98 Let me double-check with my team; I’ll get back to you soon.

@danielkang98 We recommend that instead of using the update community function, could you try the following code instead:

await AmitySocialClient.newCommunityRepository()
    .membership(communityId)
    .addMembers(userIds)
    .then((members) {})
    .onError((error, stackTrace) async {
  //handle error
  await AmityDialog()
      .showAlertErrorDialog(title: "Error!", message: error.toString());
});

1 Like

Thank you above worked for updating community member count. However when trying retrieve community members, with below code _queryChannelMembers I am not retrieving anything, what is the correct way to retrieve all the members in the community?

  void _queryChannelMembers() {
    _channelMembersController = PagingController(
      pageFuture: (token) => AmityChatClient.newChannelRepository()
          .membership(widget.community.channelId!)
          .getMembers()
          .getPagingData(token: token, limit: 20),
      pageSize: 20,
    )..addListener(
        () {
          if (_channelMembersController.error == null) {
            //handle results, we suggest to clear the previous items
            //and add with the latest _postController.loadedItems
            _amityChannelMembers.clear();
            _amityChannelMembers.addAll(_channelMembersController.loadedItems);
            //update widgets d
          } else {
            //error on pagination controller
            //update widgets
          }
        },
      );
  }

@amitysupport any update for above?

@danielkang98 Regarding your issue, we recommend that you follow these instructions: Query Community Members - Amity Docs.

Hello, yes I have checked the query community members documentation. However how would I retrieve all members in a community. Is below code the right way to retrieve members from a specific channel? Because I am not retrieving anything when calling below code.

void _queryChannelMembers() {
    _channelMembersController = PagingController(
      pageFuture: (token) => AmityChatClient.newChannelRepository()
          .membership(widget.community.channelId!)
          .getMembers()
          .getPagingData(token: token, limit: 20),
      pageSize: 20,
    )..addListener(
        () {
          if (_channelMembersController.error == null) {
            //handle results, we suggest to clear the previous items
            //and add with the latest _postController.loadedItems
            _amityChannelMembers.clear();
            _amityChannelMembers.addAll(_channelMembersController.loadedItems);
            //update widgets d
          } else {
            //error on pagination controller
            //update widgets
          }
        },
      );
  }

@amitysupport Can I get help for mentioned above?

Hello, to query channel members, please see the example below:

final _amityChannelMembers = <AmityChannelMember>[];
late PagingController<AmityChannelMember> _channelMembersController;

void queryChannelMembers(String channelId, String keyword) {
  _channelMembersController = PagingController(
    pageFuture: (token) => AmityChatClient.newChannelRepository()
        .membership(channelId)
        .searchMembers(keyword)
        .getPagingData(token: token, limit: 20),
    pageSize: 20,
  )..addListener(
      () {
        if (_channelMembersController.error == null) {
          //handle results, we suggest to clear the previous items
          //and add with the latest _controller.loadedItems
          _amityChannelMembers.clear();
          _amityChannelMembers.addAll(_channelMembersController.loadedItems);
          //update widgets
        } else {
          //error on pagination controller
          //update widgets
        }

            WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      _channelMembersController.fetchNextPage();
    });
     scrollcontroller.addListener(loadnextpage);
      },
    );
}

  void loadnextpage() {
    print("load next page");
    if ((scrollcontroller.position.pixels ==
            scrollcontroller.position.maxScrollExtent) &&
        _channelMembersController.hasMoreItems) {
      _channelMembersController.fetchNextPage();
    }
  }

Docs: Query Members - Amity Docs

How can I get all the amityChannelMembers? I have tried the code below but I still do not receive any amitychannelmembers. I have also tried using the method getMembers() but that did not retrieve all amityChannelMembers in a channel.

searchMembers(“”) //with Empty String

AmityChatClient.newChannelRepository()
        .membership(channelId)
        .searchMembers("") //EMPTY SO THAT IT RETURNS ALL USERS
        .getPagingData(token: token, limit: 20)

getMembers()

      pageFuture: (token) => AmityChatClient.newChannelRepository()
          .membership(widget.community.channelId!)
          .getMembers()
          .getPagingData(token: token, limit: 20),
      pageSize: 20,
    )..addListener(
        () {
          if (_channelMembersController.error == null) {
            //handle results, we suggest to clear the previous items
            //and add with the latest _postController.loadedItems
            _amityChannelMembers.clear();
            _amityChannelMembers.addAll(_channelMembersController.loadedItems);
            //update widgets d
          } else {
            //error on pagination controller
            //update widgets
          }
        },
      );
  }

@danielkang98 When you use getMembers(), do you receive some amityChannelMembers but not all of them?

No I do not receive any.

Even calling

   WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      _channelMembersController.fetchNextPage();
      _postController.fetchNextPage();
    });

Does not receive any.

@danielkang98 Okay i’ll pass this to check with my team.

@amitysupport
Any update?

What we are trying to accomplish is to show members profile in the selected community. Thus using getMembers to retrieve all the Amity users information (such as profile url). How can I accomplish this?

Hello, to query community member, please refer to this section: Query Community Members | Amity Docs