Skip to content

Commit 12c54ec

Browse files
committed
update demo
1 parent 25d460b commit 12c54ec

File tree

1 file changed

+22
-75
lines changed

1 file changed

+22
-75
lines changed

example/lib/main.dart

Lines changed: 22 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
import 'dart:convert' as convert;
2-
import 'dart:convert';
31
import 'package:flutter/material.dart';
42
import 'package:im_flutter_sdk/im_flutter_sdk.dart';
5-
import 'package:http/http.dart' as http;
63

74
void main() {
85
runApp(const MyApp());
@@ -172,6 +169,7 @@ class _MyHomePageState extends State<MyHomePage>
172169
}
173170

174171
try {
172+
_addLogToConsole("begin login...");
175173
await EMClient.getInstance.login(_username, _password);
176174
_addLogToConsole("login succeed, username: $_username");
177175
} on EMError catch (e) {
@@ -180,13 +178,30 @@ class _MyHomePageState extends State<MyHomePage>
180178
}
181179

182180
void _signOut() async {
183-
await EMClient.getInstance.logout(true);
181+
try {
182+
_addLogToConsole("begin logout...");
183+
await EMClient.getInstance.logout(true);
184+
_addLogToConsole("logout succeed, username: $_username");
185+
} on EMError catch (e) {
186+
_addLogToConsole(
187+
"logout failed, code: ${e.code}, desc: ${e.description}");
188+
}
184189
}
185190

186191
void _signUp() async {
187-
Map<String, int> users = await EMClient.getInstance.groupManager
188-
.fetchMuteListFromServer("184423144292354");
189-
debugPrint("users $users");
192+
if (_username.isEmpty || _password.isEmpty) {
193+
_addLogToConsole("username or password is null");
194+
return;
195+
}
196+
197+
try {
198+
_addLogToConsole("begin create account...");
199+
await EMClient.getInstance.createAccount(_username, _password);
200+
_addLogToConsole("create account succeed, username: $_username");
201+
} on EMError catch (e) {
202+
_addLogToConsole(
203+
"create account failed, code: ${e.code}, desc: ${e.description}");
204+
}
190205
}
191206

192207
void _sendMessage() async {
@@ -313,71 +328,3 @@ class _MyHomePageState extends State<MyHomePage>
313328
@override
314329
void onMessageReactionDidChange(List<EMMessageReactionChange> list) {}
315330
}
316-
317-
class HttpRequestManager {
318-
static String host = "a41.easemob.com";
319-
static String registerUrl = "/app/chat/user/register";
320-
static String loginUrl = "/app/chat/user/login";
321-
322-
static Future<bool> registerToAppServer({
323-
required String username,
324-
required String password,
325-
}) async {
326-
bool ret = false;
327-
Map<String, String> params = {};
328-
params["userAccount"] = username;
329-
params["userPassword"] = password;
330-
331-
var uri = Uri.https(host, registerUrl);
332-
333-
var client = http.Client();
334-
335-
var response = await client.post(
336-
uri,
337-
headers: {'Content-Type': 'application/json'},
338-
body: jsonEncode(params),
339-
);
340-
341-
do {
342-
if (response.statusCode != 200) {
343-
break;
344-
}
345-
Map<String, dynamic>? map = convert.jsonDecode(response.body);
346-
if (map != null) {
347-
if (map["code"] == "RES_OK") {
348-
ret = true;
349-
}
350-
}
351-
} while (false);
352-
353-
return ret;
354-
}
355-
356-
static Future<String?> loginToAppServer({
357-
required String username,
358-
required String password,
359-
}) async {
360-
Map<String, String> params = {};
361-
params["userAccount"] = username;
362-
params["userPassword"] = password;
363-
364-
var uri = Uri.https(host, loginUrl);
365-
366-
var client = http.Client();
367-
368-
var response = await client.post(
369-
uri,
370-
headers: {'Content-Type': 'application/json'},
371-
body: jsonEncode(params),
372-
);
373-
if (response.statusCode == 200) {
374-
Map<String, dynamic>? map = convert.jsonDecode(response.body);
375-
if (map != null) {
376-
if (map["code"] == "RES_OK") {
377-
return map["accessToken"];
378-
}
379-
}
380-
}
381-
return null;
382-
}
383-
}

0 commit comments

Comments
 (0)