@@ -98,7 +98,7 @@ class _MyHomePageState extends State<MyHomePage> {
9898
9999 ScrollController scrollController = ScrollController();
100100 String _username = "";
101- String _password = "";
101+ String _token = "";
102102 String _messageContent = "";
103103 String _chatId = "";
104104 final List<String> _logText = [];
@@ -127,8 +127,8 @@ class _MyHomePageState extends State<MyHomePage> {
127127 onChanged: (username) => _username = username,
128128 ),
129129 TextField(
130- decoration: const InputDecoration(hintText: "Enter password "),
131- onChanged: (password ) => _password = password ,
130+ decoration: const InputDecoration(hintText: "Enter token "),
131+ onChanged: (token ) => _token = token ,
132132 ),
133133 const SizedBox(height: 10),
134134 Row(
@@ -158,18 +158,6 @@ class _MyHomePageState extends State<MyHomePage> {
158158 ),
159159 ),
160160 ),
161- const SizedBox(width: 10),
162- Expanded(
163- child: TextButton(
164- onPressed: _signUp,
165- child: const Text("SIGN UP"),
166- style: ButtonStyle(
167- foregroundColor: MaterialStateProperty.all(Colors.white),
168- backgroundColor:
169- MaterialStateProperty.all(Colors.lightBlue),
170- ),
171- ),
172- ),
173161 ],
174162 ),
175163 const SizedBox(height: 10),
@@ -218,9 +206,6 @@ class _MyHomePageState extends State<MyHomePage> {
218206 void _signOut() async {
219207 }
220208
221- void _signUp() async {
222- }
223-
224209 void _sendMessage() async {
225210 }
226211
@@ -243,9 +228,9 @@ class _MyHomePageState extends State<MyHomePage> {
243228
244229``` dart
245230void _initSDK() async {
246- EMOptions options = EMOptions(
247- appKey: "<#Your AppKey#>",
248- autoLogin: false,
231+ EMOptions options = EMOptions.withAppKey (
232+ "<#Your AppKey#>",
233+ autoLogin: false,
249234 );
250235 await EMClient.getInstance.init(options);
251236 // 通知 SDK UI 已准备好。该方法执行后才会收到 `EMChatRoomEventHandler`、`EMContactEventHandler` 和 `EMGroupEventHandler` 回调。
@@ -259,19 +244,20 @@ void _initSDK() async {
259244
260245在生产环境中,为了安全考虑,你需要在你的应用服务器集成 [ 获取 App Token API] ( /document/server-side/easemob_app_token.html ) 和 [ 获取用户 Token API] ( /document/server-side/easemob_user_token.html ) 实现获取 Token 的业务逻辑,使你的用户从你的应用服务器获取 Token。
261246
247+
262248### 添加登录
263249
264250在 ` _signIn ` 方法中添加登录代码。
265251
266252``` dart
267253void _signIn() async {
268- if (_username.isEmpty || _password .isEmpty) {
269- _addLogToConsole("username or password is null");
254+ if (_username.isEmpty || _token .isEmpty) {
255+ _addLogToConsole("username or token is null");
270256 return;
271257 }
272258
273259 try {
274- await EMClient.getInstance.login (_username, _password );
260+ await EMClient.getInstance.loginWithToken (_username, _token );
275261 _addLogToConsole("sign in succeed, username: $_username");
276262 } on EMError catch (e) {
277263 _addLogToConsole("sign in failed, e: ${e.code} , ${e.description}");
@@ -445,18 +431,3 @@ flutter run
445431运行结果如下:
446432
447433<img src =" /images/flutter/simulator_screen_shot1.png " width =" 500 " />
448-
449- 参考以下步骤发送和接收文本消息:
450-
451- 1 . 输入任意用户名(如 ` flutter001 ` 和 ` flutter002 ` )和密码 ` 1 ` ,点击 ` SIGN UP ` 创建用户;
452- 2 . 以 ` flutter001 ` 身份登录 Demo,将 ` Enter the username you want to send ` 输如为 ` flutter002 ` , 发送文本消息;
453-
454- <img src =" /images/flutter/simulator_screen_shot2.png " width =" 500 " />
455-
456- 3 . 以 ` flutter002 ` 身份登录 Demo,查看 Log 信息确认是否都到消息。
457-
458- <img src =" /images/flutter/simulator_screen_shot3.png " width =" 500 " />
459-
460- ## 后续步骤
461-
462- 为保障通信安全,在正式生产环境中,你需要在自己的 app 服务端生成 Token。详见[ 使用 App Token 鉴权] ( /product/easemob_app_token.html ) 。
0 commit comments