You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Agora RTM登录失败求助:出现AgoraRtmClientException错误

解决Agora RTM登录报错AgoraRtmClientException的方案

一、先获取精准错误信息

你当前代码仅打印异常实例,无法定位具体问题。先修改登录方法的捕获逻辑,输出完整错误码和描述:

void _login(BuildContext context) async {
  String userId = _userId.text;
  if (userId.isEmpty) {
    print('Please input your user id to login.');
    return;
  }

  try {
    await _client!.login(null, userId);
    logController.addLog('Login success: ' + userId);
    _joinChannel(context);
  } catch (error) {
    if (error is AgoraRtmClientException) {
      print('Login error code: ${error.code}, message: ${error.message}');
    } else {
      print('Login error: $error');
    }
  }
}

常见错误码对应问题:

  • 10004:AppID无效或未开启RTM服务
  • 10007:用户ID格式违规(为空、含特殊字符)
  • 10011:网络连接失败

二、核心配置排查

  • AppID有效性:确认appId是Agora控制台获取的正确值,且该项目已开启RTM服务(控制台→项目管理→编辑项目→启用RTM)。
  • 用户ID合规:用户ID需为1-64位字符串,不能含空格、特殊符号(如@#$%),且同一ID不能重复登录。
  • 网络权限:
    • Android:在AndroidManifest.xml添加<uses-permission android:name="android.permission.INTERNET"/>
    • iOS:在Info.plist配置网络权限,确保允许网络访问

三、修复客户端初始化逻辑

当前initState中调用异步的_createClient,可能出现_client未初始化完成就触发登录的情况。修改如下:

void _createClient() async {
  _client = await AgoraRtmClient.createInstance(appId);
  _client!.onMessageReceived = (AgoraRtmMessage message, String peerId) {
    logController.addLog("Private Message from " + peerId + ": " + message.text);
  };
  _client!.onConnectionStateChanged = (int state, int reason) {
    logController.addLog('Connection state changed: ' + state.toString() + ', reason: ' + reason.toString());
    if (state == 5) {
      _client!.logout();
      logController.addLog('Logout.');
    }
  };
  setState(() {}); // 通知UI客户端已初始化
}

同时禁用未初始化时的登录按钮:

OutlinedButton(
  child: Text('Login'),
  onPressed: _client == null ? null : () => _login(context),
),

四、控制台配置检查

  • 确认RTM服务状态:控制台中项目的RTM服务是否正常启用,免费套餐需注意并发用户数限制。
  • IP白名单:若控制台设置了IP白名单,确保测试设备IP在允许列表内。

五、更新SDK版本

确保agora_rtm插件为最新稳定版,旧版本可能存在已知bug。在pubspec.yaml中更新依赖:

dependencies:
  agora_rtm: ^latest_version

执行flutter pub get完成更新。


内容的提问来源于stack exchange,提问作者Homerunner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 14:57:04