flutter 融云 sdk

…衆ロ難τιáo~ 提交于 2020-12-04 13:36:08

###flutter 融云 sdk
###前言
1.集成 flutter 融云 sdk ,需要一个稳定的 flutter 环境,能正常的创建和运行项目。
2.前期准备融云官网申请开发者账号
通过管理后台的 "基本信息"->"App Key" 获取 AppKey
3.通过管理后台的 "IM 服务"—>"API 调用"->"用户服务"->"获取 Token",通过用户 id 获取 IMToken
4.我知道没图是骗不到人的。先放图,大家看一下最终实现的效果。
BsvRVU.jpg






集成 sdk

  1. 依赖 IM Flutter plugin在项目的 pubspec.yaml 中写如下依赖。
dependencies:
  flutter:
    sdk: flutter

rongcloud_im_plugin: ^4.0.3
  1. 然后在项目路径执行 flutter packages get 来下载 Flutter Plugin。
  2. 我们写 2 个 button 和 2 个 text ,分别用来实现 init 和 connect 的事件和状态。
    4.初始化 SDK
    RongIMClient.init(RongAppKey);

    5.连接 IM

    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.br/>@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 ){











}else{
_isConnect="连接失败";
}
});
});
}
@override
br/>_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("连接"),
        ),
      ],
    ),
  ),
);

}
}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!