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("鏈接"), ), ], ), ), ); } }