1.集成 flutter 融云 sdk ,须要一个稳定的 flutter 环境,能正常的建立和运行项目。
2.前期准备融云官网申请开发者帐号
经过管理后台的 "基本信息"->"App Key" 获取 AppKey
3.经过管理后台的 "IM 服务"—>"API 调用"->"用户服务"->"获取 Token",经过用户 id 获取 IMToken
4.我知道没图是骗不到人的。先放图,你们看一下最终实现的效果。数据库
dependencies: flutter: sdk: flutter rongcloud_im_plugin: ^4.0.3
4.初始化 SDKapp
RongIMClient.init(RongAppKey);
5.链接 IMless
RongIMClient.connect(RongIMToken, (int code, String userId) { print('connect result ' + code.toString()); EventBus.instance.commit(EventKeys.UpdateNotificationQuietStatus, {}); if (code == 31004 || code == 12) { Navigator.of(context).pushAndRemoveUntil(new MaterialPageRoute(builder: (context) => new LoginPage()), (route) => route == null); } else if (code == 0) { print("connect userId" + userId); // 链接成功后打开数据库 // _initUserInfoCache(); }
import 'package:flutter/material.dart'; import 'package:rongcloud_im_plugin/rongcloud_im_plugin.dart' as prefix; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( primarySwatch: Colors.blue, visualDensity: VisualDensity.adaptivePlatformDensity, ), home: MyHomePage(title: 'Flutter Demo Home Page'), ); } } class MyHomePage extends StatefulWidget { MyHomePage({Key key, this.title}) : super(key: key); final String title; @override _MyHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { var _isInit ; var _isConnect ; void _init() { setState(() { prefix.RongIMClient.init("pvxdm17jpof6r"); _isInit="已经初始化"; }); } void _connect() { setState(() { prefix.RongIMClient.connect("rbdI/5jrPxR4aQ2078HhWnHte7+VrAhsnSjOcYQ3SKOCXhodQlcZYZ5acv4syCtN0dsYRNvxZh44fo4VR5s+6A==", (code, userId){ if(code == 0 ){ _isConnect="链接成功"; }else{ _isConnect="链接失败"; } }); }); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( // Here we take the value from the MyHomePage object that was created by // the App.build method, and use it to set our appbar title. title: Text(widget.title), ), body: Center( // Center is a layout widget. It takes a single child and positions it // in the middle of the parent. child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Text( '$_isInit', style: Theme.of(context).textTheme.headline4, ), Text( '$_isConnect', style: Theme.of(context).textTheme.headline4, ), MaterialButton( minWidth: 250.0, onPressed: () {_init();}, colorBrightness: Brightness.dark, color: Colors.deepPurpleAccent, elevation: 20.0, splashColor: Colors.green, //highlightColor: Colors.red, highlightElevation: 1.0, child: Text("初始化"), ), MaterialButton( minWidth: 250.0, onPressed: () {_connect();}, colorBrightness: Brightness.dark, color: Colors.deepPurpleAccent, elevation: 20.0, splashColor: Colors.green, //highlightColor: Colors.red, highlightElevation: 1.0, child: Text("链接"), ), ], ), ), ); } }