为什么不能在 flutter 中使用流聊天 api 创建用户?
why can't create user using stream chat api in flutter?
我正在尝试使用 Stream chat API
在我的 flutter 应用程序中创建一个聊天屏幕。问题是当我尝试创建一个有两个用户的频道时,它显示即使我创建了用户也没有创建:
E/flutter ( 4695): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception:
StreamChatNetworkError(code: 4, message: GetOrCreateChannel failed with error: "The following
users are involved in channel create operation, but don't exist: [hasan11 mohammed11]. Please
create the user objects before setting up the channel.", statusCode: 400, data:
ErrorResponse(code: 4, message: GetOrCreateChannel failed with error: "The following users are
involved in channel create operation, but don't exist: [hasan11 mohammed11]. Please create the
user objects before setting up the channel.", statusCode: 400, moreInfo:
https://getstream.io/chat/docs/api_errors_response))
这是我用于初始化频道和用户的飞镖代码:
onPressed () {
await streamAPI.initUser(client,
username: 'hasan',
id: Config.hasanID,
token: Config.hasanToken);
final channel = await streamAPI.createChannel(
client, 'messaging', 'sample', [Config.hasanID, Config.mohammedID]);
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) =>
chat(client: client, channel: channel)));
}
StreamAPI.dart代码:
import 'package:flutter/cupertino.dart';
import 'package:stream_chat_flutter/stream_chat_flutter.dart';
class streamAPI {
static Future initUser(
StreamChatClient client, {
@required String username,
@required String id,
@required String token
}) async {
final user = User(
id: id,
extraData: {
'name': username
}
);
await client.connectUser(user, token);
}
static Future<Channel> createChannel(
StreamChatClient client,
@required String type,
@required String id,
List<String> idMembers
) async {
final channel = client.channel(type, id: id, extraData: {
'name': 'channel1',
'members': idMembers
});
await channel.create();
channel.watch();
return channel;
}
}
有人能帮帮我吗?
确保这些用户是在 Stream 后端创建的。在您的 Stream 仪表板中,转到 Explorer -> users 部分,您应该会看到所有用户的列表。确保 IDs
与您的代码中的匹配。
Stream Dashboard Example
请注意,您不需要同时调用 create
和 watch
,因为 watch
会自动创建频道。
如果您使用的是 Stream Chat UI 或核心组件,您甚至不需要自己调用 watch
,那么您只需要调用 create
。
如果您仍然卡住,我建议您查看 Flutter 教程页面:https://getstream.io/chat/flutter/tutorial/
或者 Stream Flutter YouTube 播放列表:https://www.youtube.com/watch?v=pO_MOJRqYlk&list=PLNBhvhkAJG6t-BxkRAnSqa67lm5C1mpKk
我正在尝试使用 Stream chat API
在我的 flutter 应用程序中创建一个聊天屏幕。问题是当我尝试创建一个有两个用户的频道时,它显示即使我创建了用户也没有创建:
E/flutter ( 4695): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception:
StreamChatNetworkError(code: 4, message: GetOrCreateChannel failed with error: "The following
users are involved in channel create operation, but don't exist: [hasan11 mohammed11]. Please
create the user objects before setting up the channel.", statusCode: 400, data:
ErrorResponse(code: 4, message: GetOrCreateChannel failed with error: "The following users are
involved in channel create operation, but don't exist: [hasan11 mohammed11]. Please create the
user objects before setting up the channel.", statusCode: 400, moreInfo:
https://getstream.io/chat/docs/api_errors_response))
这是我用于初始化频道和用户的飞镖代码:
onPressed () {
await streamAPI.initUser(client,
username: 'hasan',
id: Config.hasanID,
token: Config.hasanToken);
final channel = await streamAPI.createChannel(
client, 'messaging', 'sample', [Config.hasanID, Config.mohammedID]);
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) =>
chat(client: client, channel: channel)));
}
StreamAPI.dart代码:
import 'package:flutter/cupertino.dart';
import 'package:stream_chat_flutter/stream_chat_flutter.dart';
class streamAPI {
static Future initUser(
StreamChatClient client, {
@required String username,
@required String id,
@required String token
}) async {
final user = User(
id: id,
extraData: {
'name': username
}
);
await client.connectUser(user, token);
}
static Future<Channel> createChannel(
StreamChatClient client,
@required String type,
@required String id,
List<String> idMembers
) async {
final channel = client.channel(type, id: id, extraData: {
'name': 'channel1',
'members': idMembers
});
await channel.create();
channel.watch();
return channel;
}
}
有人能帮帮我吗?
确保这些用户是在 Stream 后端创建的。在您的 Stream 仪表板中,转到 Explorer -> users 部分,您应该会看到所有用户的列表。确保 IDs
与您的代码中的匹配。
Stream Dashboard Example
请注意,您不需要同时调用 create
和 watch
,因为 watch
会自动创建频道。
如果您使用的是 Stream Chat UI 或核心组件,您甚至不需要自己调用 watch
,那么您只需要调用 create
。
如果您仍然卡住,我建议您查看 Flutter 教程页面:https://getstream.io/chat/flutter/tutorial/
或者 Stream Flutter YouTube 播放列表:https://www.youtube.com/watch?v=pO_MOJRqYlk&list=PLNBhvhkAJG6t-BxkRAnSqa67lm5C1mpKk