loadMessagesWithKeyword method

Future<List<ChatMessage>> loadMessagesWithKeyword(
  1. String keywords, {
  2. @Deprecated('The `sender` parameter is deprecated, use `senders` instead.') String? sender,
  3. List<String>? senders,
  4. int timestamp = -1,
  5. int count = 20,
  6. MessageSearchScope searchScope = MessageSearchScope.All,
  7. ChatSearchDirection direction = ChatSearchDirection.Up,
})

根据消息中的关键词、消息时间戳、要搜索的消息条数、搜索范围和搜索方向从 SDK 本地数据库中搜索指定数量的消息。

注意:若搜索的消息条数非常大,需要考虑内存消耗。

Param keywords 搜索消息中的关键词。

Param senders 消息发送方列表。若不设置该参数,SDK 搜索消息时会忽略该参数。

Param timestamp 搜索的起始消息时间戳。

Param count 搜索的消息条数。

Param searchScope 消息搜索范围,详见 MessageSearchScope

Param direction 消息搜索方向。

Return 消息列表。

Throws 如果有异常会在这里抛出,包含错误码和错误描述,详见 ChatError

Implementation

Future<List<ChatMessage>> loadMessagesWithKeyword(
  String keywords, {
  @Deprecated('The `sender` parameter is deprecated, use `senders` instead.')
  String? sender,
  List<String>? senders,
  int timestamp = -1,
  int count = 20,
  MessageSearchScope searchScope = MessageSearchScope.All,
  ChatSearchDirection direction = ChatSearchDirection.Up,
}) async {
  try {
    Map req = _toJson();
    req["keywords"] = keywords;
    req['count'] = count;
    req['timestamp'] = timestamp;
    req['searchScope'] = MessageSearchScope.values.indexOf(searchScope);
    req['direction'] = direction.index;
    req.putIfNotNull("senders", senders);
    req.putIfNotNull("from", sender);

    Map<String, dynamic> result = await platform_interface.Client.instance.conversationManager
        .callNativeMethod(ChatMethodKeys.loadMsgWithKeywords, req);
    ChatError.hasErrorFromResult(result);
    List<ChatMessage> msgList = [];
    result[ChatMethodKeys.loadMsgWithKeywords]?.forEach((element) {
      msgList.add(ChatMessage.fromJson(element));
    });
    return msgList;
  } catch (e) {
    rethrow;
  }
}