Flutter 超级群频道与用户组用法

本页给出 Flutter 端从“超级群 → 频道 → 用户组 → 按 SUPER_GROUP(10) 收发消息”的完整用法,示例使用 SDK 公开方法(impaas_imlib),并标注版本与限制。语义与兼容规则见 超级群会话类型。

项内容
包 / 模块impaas_imlib
超级群类型常量RongIMConversationType.SUPER_GROUP(数值 10;旧版本 SDK 可用数值 10 或继续 2+super)
调用风格Future<RcResult<T>>;`res.code != 0` 为业务失败
频道/用户组操作8 个语义操作(创建/列表/加入/离开频道、创建/列表用户组、绑定、按用户反查)
消息频道消息按超级群会话发送;rev23 起可带 options.channelId 按频道投递(不带则群级);10 与存量 2+super 共用同一会话键
版本前提

示例中的 SUPER_GROUP 常量与“按 10 发送”需要 SDK/服务端包含 rev22。

若当前环境尚未升级:① 用数值 10 传参(仍有实现拒绝风险);② 继续用 GROUP(2) + groupType=super(存量兼容写法)。

频道/用户组接口(rev21)不依赖 10,可直接使用。

前置条件​

  • 应用已开通 supergroup 服务(未开通返回 1021);客户端已连接(CONNECTED)。
  • 超级群成员上限 100000;频道/用户组创建需要群主或管理员权限(否则 1003)。
  • 频道列表/用户组列表需要群成员身份;频道 ID 在应用内唯一,跨群同名时用 groupId 消歧。

步骤 1:创建/确认超级群​

Dart
// 1) 创建/确认超级群(复用 createGroup,options.groupType=super)
final created = await client.createGroup(
  '产品社区', ['alice', 'bob'],
  groupId: 'g_1001',
  options: const GroupOptions(groupType: RongIMGroupType.SUPER),
);
if (created.code != 0) throw Exception(created.msg);

步骤 2:频道管理​

Dart
// 2) 频道:创建 → 列表 → 加入 / 离开
final channel = await client.createSupergroupChannel(CreateSupergroupChannelParams(
  groupId: 'g_1001',
  name: '产品讨论',
  type: RongIMChannelType.PUBLIC,
));
if (channel.code != 0) throw Exception(channel.msg);
final channelId = channel.data!.channelId;

final list = await client.listSupergroupChannels('g_1001');
print(list.data?.channels);

await client.joinSupergroupChannel(channelId, options: SupergroupChannelOptions(groupId: 'g_1001'));
await client.leaveSupergroupChannel(channelId, options: SupergroupChannelOptions(groupId: 'g_1001'));

步骤 3:用户组与绑定​

Dart
// 3) 用户组:创建 → 绑定频道 → 反查
final ug = await client.createUserGroup(CreateUserGroupParams(
  groupId: 'g_1001', name: '核心成员', members: ['alice', 'bob'],
));
await client.bindUserGroupToChannel(
  channelId, ug.data!.userGroupId,
  options: SupergroupChannelOptions(groupId: 'g_1001'),
);

final groups = await client.listUserGroups('g_1001');
final mine = await client.getUserGroupsByUser('alice', options: SupergroupChannelOptions(groupId: 'g_1001'));

步骤 4:按 10 收发与会话​

Dart
// 4) 按 10 发送 / 历史 / 会话列表 / 事件
const superGroup = 10; // RongIMConversationType.SUPER_GROUP(rev22)
final sent = await client.sendTextMessage(superGroup, 'g_1001', '大家好');
if (sent.code != 0) throw Exception(sent.msg);

final history = await client.getHistoryMessages(superGroup, 'g_1001', limit: 20);
final conversations = await client.getConversationList();

client.addEventListener(RongIMEvents.MESSAGES, (payload) {
  for (final message in payload.messages) {
    final isSuper = message.conversationType == 10 ||
        (message.conversationType == 2 && groupTypeOf(message.targetId) == "super");
    if (isSuper) renderSuperGroupMessage(message);
  }
});

接口与错误码​

操作入参返回错误码
createSupergroupChannelparams:{groupId:string, name:string, type:ChannelType, joinPolicy?:JoinPolicy}SupergroupChannel1001 1002 1003 1005 1015
listSupergroupChannelsgroupId:string{groupId:string, channels:SupergroupChannel[], total:uint32}1001 1002 1003 1005
joinSupergroupChannelchannelId:string, options:{groupId?:string}?{groupId:string, channelId:string, userId:string, joined:bool, alreadyJoined:bool}1001 1002 1003 1005
leaveSupergroupChannelchannelId:string, options:{groupId?:string}?{groupId:string, channelId:string, userId:string, left:bool, wasMember:bool}1001 1002 1003 1005
bindUserGroupToChannelchannelId:string, userGroupId:string, options:{groupId?:string}?{groupId:string, channelId:string, userGroupId:string, bound:bool, reverseIndexed:bool}1001 1002 1003 1005
createUserGroupparams:{groupId:string, name:string, members?:string[]}UserGroup1001 1002 1003 1004 1015
listUserGroupsgroupId:string{groupId:string, userGroups:UserGroup[], total:uint32}1001 1002 1003 1005
getUserGroupsByUseruserId:string, options:{groupId?:string}?{userId:string, userGroups:UserGroup[], total:uint32}1001 1002 1003
错误码名称含义可重试
1001INVALID_PARAMETER参数错误否
1002INVALID_TOKENToken 无效或已过期否
1003FORBIDDEN无权限否
1004USER_NOT_FOUND用户不存在否
1005GROUP_NOT_FOUND群组不存在否
1006NOT_GROUP_MEMBER未加入群组否
1015MEMBERSHIP_LIMIT成员数量超出上限否

限制​

  • 频道消息已支持(rev23):发送传 options.channelId 即按频道投递;不带 channelId 为群级消息。频道消息不单独成会话,未读/回执按群会话聚合。
  • 不自动创建默认频道(RCDefault),频道需要显式创建;默认免打扰未实现。
  • 单群频道与用户组各 100 个上限(超限 1015);频道名/用户组名长度在接口内校验(1001)。
  • IMKit 未提供频道/用户组组件;10 的会话渲染需要 IMKit 升级到识别 rev22 的版本。
  • iOS / HarmonyOS 为契约预留(声明同构,无运行时代码)。

相关链接​