loadMessagesWithKeyword method
- String keywords, {
- String? sender,
- int timestamp = -1,
- int count = 20,
- MessageSearchScope searchScope = MessageSearchScope.All,
- ChatSearchDirection direction = ChatSearchDirection.Up,
~english Loads messages from the local database by the following parameters: keywords, timestamp, max count, sender, search direction.
Note Pay attention to the memory usage when the maxCount is large.
Param keywords
The keywords in message.
Param sender
The message sender. The param can also be used to search in group chat.
Param timestamp
The timestamp for search.
Param count
The maximum number of messages to search.
Param searchScope
The message search scope. See MessageSearchScope.
Param direction
The direction in which the message is loaded: ChatSearchDirection.
ChatSearchDirection.Up
: Gets the messages loaded before the timestamp of the specified message ID.
ChatSearchDirection.Down
: Gets the messages loaded after the timestamp of the specified message ID.
Returns The list of retrieved messages.
Throws A description of the exception. See ChatError. ~end
~chinese 根据消息中的关键词、搜索消息的时间点、搜索结果的最大条数、搜索来源和搜索方向从 SDK 本地数据库中搜索指定数量的消息。
注意:当 maxCount 非常大时,需要考虑内存消耗。
Param keywords
搜索消息中的关键词。
Param sender
消息发送方(用户、群组或聊天室)。
Param timestamp
搜索消息的时间点。
Param count
搜索结果的最大条数。
Param searchScope
消息搜索范围,详见 MessageSearchScope。
Param direction
消息搜索方向。
Return 消息列表。
Throws 如果有异常会在这里抛出,包含错误码和错误描述,详见 ChatError。 ~end
Implementation
Future<List<ChatMessage>> loadMessagesWithKeyword(
String keywords, {
String? sender,
int timestamp = -1,
int count = 20,
MessageSearchScope searchScope = MessageSearchScope.All,
ChatSearchDirection direction = ChatSearchDirection.Up,
}) async {
Map req = this._toJson();
req["keywords"] = keywords;
req['count'] = count;
req['timestamp'] = timestamp;
req['searchScope'] = MessageSearchScope.values.indexOf(searchScope);
req['direction'] = direction == ChatSearchDirection.Up ? "up" : "down";
req.putIfNotNull("sender", sender);
Map<String, dynamic> result = await _emConversationChannel.invokeMethod(
ChatMethodKeys.loadMsgWithKeywords, req);
try {
ChatError.hasErrorFromResult(result);
List<ChatMessage> msgList = [];
result[ChatMethodKeys.loadMsgWithKeywords]?.forEach((element) {
msgList.add(ChatMessage.fromJson(element));
});
return msgList;
} on ChatError catch (e) {
throw e;
}
}