跳到主要内容

SDK初始化

更新时间:2026-04-03 11:20:37

SDK初始化

APP运行期间只能初始化一次,建议在App启动时初始化。建议在main.dart的main函数中,初始化后再执行runApp。

// 导入头文件
import 'package:airtc_engine/src/iot_smart/rtcx_iot_smart.dart';
final iotSmartApi = RTCXFluIotSmartApi();

void main() async {
WidgetsFlutterBinding.ensureInitialized();

RTCXFluInitConfig initConfig = RTCXFluInitConfig();
initConfig.regionType = RTCXFluRegionType.REGION_CHINA_ONLY;
initConfig.debug = true;
initConfig.enableMediaPlayerLog = false;
initConfig.enableRtcPlayerLog = false;
if (Platform.isIOS) {
initConfig.appKeyConfig = RTCXFluAppKeyConfig(
appKey: "平台分配的iOS端AppKey",
appSecret: "平台分配的iOS端AppSecret",
);
} else if (Platform.isAndroid) {
initConfig.appKeyConfig = RTCXFluAppKeyConfig(
appKey: "平台分配的Android端AppKey",
appSecret: "平台分配的Android端AppSecret",
);
}
await iotSmartApi.init(initConfig);
// 注册callback manager到底层
RTCXFluDataChannelCallback.setUp(DataChannelCallbackManager());
RTCXFluThingCallback.setUp(ThingCallbackManager());
RTCXFluPlayerCallback.setUp(PlayerCallbackManager());
runApp(MyApp());
}

查看版本

  // 获取SDK版本
var version = await iotSmartApi.getVersion();
print("SDK版本: $version");

设置国家地区和RegionId

通过用户选择的国家地区,帮助您选择业务服务器最快连接的区域。 AIRTC的云端服务为多区域部署,根据您初始化SDK时config.regionType参数的取值,来判断是否需要在初始化时设置国家。

  • 如果您App运行在中国大陆地区,你需要在SDK初始化的时候,设置config.regionType = REGION_CHINA_ONLY;
  • 如果您App运行在中国大陆以外地区,你需要在SDK初始化的时候,设置config.regionType = REGION_ALL,并需要设置国家;
  1. 获取国家地区列表
  List<RTCXFluCountry> countries = await iotSmartApi.getCountryList();
print("getCountryList:${countries}");
  1. 设置国家地区
    // 获取国家地区列表后,按照需求拿到RTCXFluCountry实例后调用setCountry
RTCXFluCountry country = RTCXFluCountry(
code: '86',
isoCode: 'CHN',
areaName: '中国大陆',
domainAbbreviation: 'CN',
pinyin: 'ZhongGuoDaLu');
RTCXFluIoTResult value = await iotSmartApi.setCountry(country);
print("setCountry:${value.result}");

设置语言

通过以下设置可更改SDK内部语言。

  await iotSmartApi.setLanguage(lanaguage);

获取RegionType

通过以下设置获取SDK的RegionType

  RTCXFluRegionType regionType = await iotSmartApi.getRegionType();
print("regionType:${regionType}");

清除缓存

清理删除本地持久化存储的国家信息,当【国内切换海外】、【海外切换国内 】时必须调用此接口,否则会出现SDK接口调用失败问题

 iotSmartApi.clearCache();