共计 2776 个字符,预计需要花费 7 分钟才能阅读完成。
flutter 集成融云 sdk
前言
1. 集成 flutter 融云 sdk,须要一个稳固的 flutter 环境,能失常的创立和运行我的项目。
2. 后期筹备融云官网申请开发者账号
通过治理后盾的 “ 根本信息 ”->”App Key” 获取 AppKey
3. 通过治理后盾的 “IM 服务 ”—>”API 调用 ”->” 用户服务 ”->” 获取 Token”,通过用户 id 获取 IMToken
4. 我晓得没图是骗不到人的。先放图,大家看一下最终实现的成果。
集成 sdk
- 依赖 IM Flutter plugin 在我的项目的 pubspec.yaml 中写如下依赖。
dependencies:
flutter:
sdk: flutter
rongcloud_im_plugin: ^4.0.3
- 而后在我的项目门路执行 flutter packages get 来下载 Flutter Plugin。
- 咱们写 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.
@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("连贯"),
),
],
),
),
);
}
}
正文完