融云 Flutter IMLib
融云 IM Flutter 插件(rongcloud_im_wrapper_plugin),基于 Rust IM SDK(rust-universal-imsdk)通过
Dart FFI 封装,为 Flutter 应用提供跨平台即时通讯能力,支持 Android、iOS、鸿蒙(OHOS)、macOS、
Windows 和 Linux。
融云 IMLib 是即时通讯核心 SDK,支持单聊、群聊、聊天室、系统通知、超级群等功能。通过集成 IM Wrapper Plugin,您可以在 Flutter 应用中快速实现完整的即时通讯功能。
准备工作
- 如果您还没有融云开发者账号,在融云控制台注册一个。
- 在控制台,通过应用配置>基本信息>App Key,获取您的 App Key。
- 通过服务端 API,获取用户 Token。
App Secret 用于生成数据签名,仅在请求融云服务端 API 接口时使用。
依赖 IM Wrapper Plugin
在项目的 pubspec.yaml 中写如下依赖:
dependencies:
flutter:
sdk: flutter
rongcloud_im_wrapper_plugin: 26.23.3
插件是联邦插件:平台实现由 rongcloud_im_wrapper_plugin_platform_interface 与六个
rongcloud_im_wrapper_plugin_<平台> 包(android、ios、macos、windows、linux、ohos)提供,
由本包按版本约束自动解析,不需要在 pubspec.yaml 中手动声明这几个包。环境要求:Dart
>=2.19.6 <4.0.0、Flutter >=2.11.0。
支持平台
| 平台 | 构建方式 | 插件声明的最低版本或主要要求 |
|---|---|---|
| Android | Gradle + CMake + NDK | minSdk 24、compileSdk 36、NDK 28.2.13676358、Java 17 |
| iOS | CocoaPods + Xcode | iOS 9.0 |
| OHOS | Hvigor + CMake + OHOS Native SDK | Flutter OHOS 3.35.8+、HarmonyOS SDK 5.1.0(18),支持 arm64-v8a、x86_64 |
| macOS | CocoaPods + Xcode | macOS 10.11 |
| Linux | CMake | CMake 3.10+,需要系统 C++、pthread、zlib 等链接库 |
| Windows | CMake + Visual Studio | Visual Studio C++ 工具链,CMake 3.14+ |
实际应用可以设置高于表中值的最低系统版本。Windows、Linux、macOS 以及鸿蒙 2in1 设备可以使用 PC 风格 UI
(左侧会话列表 + 右侧聊天面板),界面由 Flutter IM Kit 的
RCKDesktopShell、RCKConvoPage、RCKChatPage 提供;本插件负责底层能力。
Linux 构建依赖
Linux 上 Wrapper 自身只需 CMake 3.10+ 与系统 C++ 运行库:生成的 librcim_flutter_ffi.so
链接 -lstdc++ -lpthread -ldl -lz -lm(需要系统 zlib 开发包)。Wrapper 还依赖
fc_native_video_thumbnail 生成小视频缩略图,其 Linux 实现要求系统安装 FFmpeg 与
libjpeg 开发包;GTK 桌面宿主(Flutter Linux 默认形态)另需 GTK 3 开发包。Debian/Ubuntu
可参考:
sudo apt-get install -y cmake build-essential pkg-config zlib1g-dev libgtk-3-dev \
libjpeg-dev libavcodec-dev libavformat-dev libavutil-dev libswscale-dev
其它发行版请安装等价开发包;缺少 FFmpeg / libjpeg 时,缩略图相关构建会失败。
支持的 CPU 架构(26.23.3 起)
| 平台 | CPU 架构 | 随包提供的动态库 |
|---|---|---|
| Android | arm64-v8a、armeabi-v7a、x86_64、x86 |
dist/<abi>/librcim_flutter_ffi.so 与 NDK 的 libc++_shared.so;宿主可用 abiFilters 只保留需要的 ABI |
| iOS | 真机 arm64;模拟器 arm64、x86_64 |
XCFramework 的 ios-arm64 与 ios-arm64_x86_64-simulator 两个 slice |
| OHOS | arm64-v8a、x86_64 |
HAR 内 libs/<abi>/librcim_flutter_ffi.so 与 OHOS NDK 的 libc++_shared.so |
| macOS | arm64、x86_64 |
XCFramework 的 macos-arm64_x86_64 通用 slice |
| Windows | x86_64 |
windows/dist/x86_64/rcim_flutter_ffi.dll |
| Linux | x86_64、arm64、loong64 |
linux/dist/<arch>/librcim_flutter_ffi.so;本地构建默认只出宿主架构,可用 RCIM_LINUX_ARCHES 一次编多个架构 |
鸿蒙(OHOS)适配
OHOS 实现(HAR 与预编译 librcim_flutter_ffi.so)随 rongcloud_im_wrapper_plugin_ohos 包自带,接入方
无需额外配置:使用 OHOS 版 Flutter(3.35.8+,官方
Flutter 无法构建 hap)按常规 OHOS 工程构建即可。宿主工程中其它 Flutter 插件的 OHOS 实现由根包
pubspec_overrides.yaml 提供,fork 按
OpenHarmony 平台 Flutter 三方库适配情况
的 <原库版本>-ohos-<ohos 版本> TAG 选取与所用 Flutter 版本匹配的版本。
错误码
26.23.3 起,RCIMIWEngine 等对外接口失败时不再返回 5.44.x 的 legacy 合成码
-101 / -102 / -103 / -104,而是返回 Rust RcimEngineError 原生码;成功仍为 0。
判断返回值时请引用公开常量类 RSIMEngineErrorCode,不要再用字面量合成码:
import 'package:rongcloud_im_wrapper_plugin/rongcloud_im_wrapper_plugin.dart';
engine.onConnected = (int? code, String? userId) {
if (code == RSIMEngineErrorCode.success) {
// 连接成功
} else if (code == RSIMEngineErrorCode.engineDropped) {
// 引擎不可用 / 已释放
}
};
| 旧 legacy 合成码 | 新原生码 | 含义 |
|---|---|---|
-101 |
33003 invalidParameter(能精确到具体参数时返回 34232 等 InvalidArgument*) |
参数错误 |
-102 |
34400 engineDropped |
引擎不可用 / 已释放 |
-103 |
原样透传原生码;无原生码时 25104 unknownServerError |
原生调用失败 |
-104 |
25104 unknownServerError |
未知错误 |
按旧合成码分支处理的代码需同步调整;完整的接口行为差异见 Legacy / Modern 接口差异台账。
快速开始
详细的集成步骤、API 使用说明、功能配置等内容,请参考 融云 IMLib Flutter 完整开发文档。
文档包含以下完整内容:
- 初始化 - SDK 初始化和引擎创建
- 连接与断开 - 建立和断开 IM 连接
- 消息管理 - 发送、接收、存储消息
- 会话管理 - 会话列表和会话操作
- 群组管理 - 群组创建、管理和操作
- 聊天室管理 - 聊天室加入和管理
- 推送服务 - 离线推送配置
- 完整的 API 参考和错误码说明
基本使用示例
1. 初始化和连接
// 导入 SDK
import 'package:rongcloud_im_wrapper_plugin/rongcloud_im_wrapper_plugin.dart';
// 初始化 SDK
RCIMIWEngineOptions options = RCIMIWEngineOptions.create();
RCIMIWEngine engine = await RCIMIWEngine.create(appkey, options);
// 连接 IM 服务
engine.connect(token, timeout);
2. 监听连接状态
engine.onConnected = (int? code, String? userId) {
if (code == 0) {
// 连接成功
}
};
3. 发送和接收消息
// 发送文本消息
RCIMIWTextMessage? textMessage = await engine.createTextMessage(
conversationType, targetId, channelId, text,
);
engine.sendMessage(textMessage);
// 监听接收消息
engine.onMessageReceived = (
RCIMIWMessage? message,
int? left,
bool? offline,
bool? hasPackage,
) {
// 处理接收到的消息
};
支持
如有任何问题,请通过以下方式获取帮助:
Introducing RC IMLib for Flutter
rongcloud_im_wrapper_plugin wraps the Rust IM SDK (rust-universal-imsdk) through Dart FFI and runs on
Android, iOS, HarmonyOS (OHOS), macOS, Windows and Linux.
RC IMLib is the core instant messaging SDK, supporting private chat, group chat, chatroom, system notifications, ultra groups, and more. By integrating the IM Wrapper Plugin, you can quickly implement complete instant messaging functionality in your Flutter applications.
RC Android IMLib Documentation
RC IMLib Flutter Complete Development Documentation
Preparations
- If you don't have a RC developer account yet, register one at RongCloud Console.
- In the console, get your App Key through Application Configuration > Basic Information > App Key.
- Get user Token through server-side API, register user.
App Secret is used to generate data signatures and is only required when calling RC's server API.
Dependencies on IM Wrapper Plugin
Add the following dependencies to your project's pubspec.yaml:
dependencies:
flutter:
sdk: flutter
rongcloud_im_wrapper_plugin: 26.23.3
The plugin is a federated plugin: its platform implementations live in
rongcloud_im_wrapper_plugin_platform_interface and six rongcloud_im_wrapper_plugin_<platform> packages
(android, ios, macos, windows, linux, ohos) and are resolved automatically through version
constraints, so you do not declare them yourself. Requires Dart >=2.19.6 <4.0.0 and Flutter >=2.11.0.
Supported platforms
| Platform | Build | Minimum version or main requirements |
|---|---|---|
| Android | Gradle + CMake + NDK | minSdk 24, compileSdk 36, NDK 28.2.13676358, Java 17 |
| iOS | CocoaPods + Xcode | iOS 9.0 |
| OHOS | Hvigor + CMake + OHOS Native SDK | Flutter OHOS 3.35.8+, HarmonyOS SDK 5.1.0(18), arm64-v8a and x86_64 |
| macOS | CocoaPods + Xcode | macOS 10.11 |
| Linux | CMake | CMake 3.10+, system C++, pthread and zlib |
| Windows | CMake + Visual Studio | Visual Studio C++ toolchain, CMake 3.14+ |
Apps may target a higher minimum version than listed. Windows, Linux, macOS and HarmonyOS 2-in-1 devices can use the PC-style UI (conversation list on the left, chat panel on the right) provided by Flutter IM Kit; this plugin provides the underlying capability.
Linux build dependencies
On Linux the wrapper itself only needs CMake 3.10+ and the system C++ runtime: the produced
librcim_flutter_ffi.so links -lstdc++ -lpthread -ldl -lz -lm (i.e. the system zlib development
package). The wrapper also depends on fc_native_video_thumbnail for short-video thumbnails, whose
Linux implementation requires the FFmpeg and libjpeg development packages; a GTK desktop host
(Flutter's default Linux form factor) additionally needs the GTK 3 development package. On Debian/Ubuntu:
sudo apt-get install -y cmake build-essential pkg-config zlib1g-dev libgtk-3-dev \
libjpeg-dev libavcodec-dev libavformat-dev libavutil-dev libswscale-dev
Install the equivalent development packages on other distributions; without FFmpeg / libjpeg the thumbnail-related builds fail.
Supported CPU architectures (since 26.23.3)
| Platform | Architectures | Shipped native libraries |
|---|---|---|
| Android | arm64-v8a, armeabi-v7a, x86_64, x86 |
dist/<abi>/librcim_flutter_ffi.so plus the NDK libc++_shared.so; keep only the ABIs you need with abiFilters |
| iOS | device arm64; simulator arm64, x86_64 |
XCFramework slices ios-arm64 and ios-arm64_x86_64-simulator |
| OHOS | arm64-v8a, x86_64 |
libs/<abi>/librcim_flutter_ffi.so plus the OHOS NDK libc++_shared.so inside the HAR |
| macOS | arm64, x86_64 |
XCFramework slice macos-arm64_x86_64 (universal) |
| Windows | x86_64 |
windows/dist/x86_64/rcim_flutter_ffi.dll |
| Linux | x86_64, arm64, loong64 |
linux/dist/<arch>/librcim_flutter_ffi.so; local builds produce the host architecture only, use RCIM_LINUX_ARCHES for several |
OHOS is self-contained: the HAR and the prebuilt librcim_flutter_ffi.so ship inside
rongcloud_im_wrapper_plugin_ohos, so no extra host configuration is required — build as a regular OHOS
project with the OpenHarmony Flutter SDK (3.35.8+; the
official Flutter cannot build hap). For the other Flutter plugins in your host project, provide their OHOS
implementations through the root pubspec_overrides.yaml, picking forks by the
<library version>-ohos-<ohos version> TAG documented in the
OpenHarmony Flutter third-party library list.
Error codes
Starting with 26.23.3, when an API such as RCIMIWEngine fails it no longer returns the legacy
synthetic codes -101 / -102 / -103 / -104 from 5.44.x; it returns the Rust
RcimEngineError native code instead. Success is still 0. Compare against the public constants in
RSIMEngineErrorCode rather than the literal synthetic codes:
import 'package:rongcloud_im_wrapper_plugin/rongcloud_im_wrapper_plugin.dart';
engine.onConnected = (int? code, String? userId) {
if (code == RSIMEngineErrorCode.success) {
// connected
} else if (code == RSIMEngineErrorCode.engineDropped) {
// engine unavailable / released
}
};
| Old legacy synthetic code | New native code | Meaning |
|---|---|---|
-101 |
33003 invalidParameter (a precise InvalidArgument* such as 34232 when the argument is known) |
Invalid parameter |
-102 |
34400 engineDropped |
Engine unavailable / released |
-103 |
The native code is passed through; 25104 unknownServerError when there is none |
Native call failed |
-104 |
25104 unknownServerError |
Unknown error |
Code that branches on the old synthetic codes must be updated; the full behaviour differences are in the Legacy / Modern API difference ledger.
Quick Start
For detailed integration steps, API usage instructions, feature configurations, and more, please refer to RC IMLib Flutter Complete Development Documentation.
The documentation includes comprehensive content on:
- Initialization - SDK initialization and engine creation
- Connect & Disconnect - Establish and disconnect IM connections
- Message Management - Send, receive, and store messages
- Conversation Management - Conversation lists and operations
- Group Management - Group creation, management, and operations
- Chatroom Management - Join and manage chatrooms
- Push Service - Offline push configuration
- Complete API reference and error code explanations
Basic Usage Examples
1. Initialize and Connect
// Import SDK
import 'package:rongcloud_im_wrapper_plugin/rongcloud_im_wrapper_plugin.dart';
// Initialize SDK
RCIMIWEngineOptions options = RCIMIWEngineOptions.create();
RCIMIWEngine engine = await RCIMIWEngine.create(appkey, options);
// Connect to IM service
engine.connect(token, timeout);
2. Listen for Connection Status
engine.onConnected = (int? code, String? userId) {
if (code == 0) {
// Connection successful
}
};
3. Send and Receive Messages
// Send text message
RCIMIWTextMessage? textMessage = await engine.createTextMessage(
conversationType, targetId, channelId, text,
);
engine.sendMessage(textMessage);
// Listen for received messages
engine.onMessageReceived = (
RCIMIWMessage? message,
int? left,
bool? offline,
bool? hasPackage,
) {
// Handle received message
};
Support
For any questions, please get help through:
- Consult the Official Complete Documentation
- Submit a ticket to contact RC technical support