loadMessagesWithKeyword method

Future<List<ChatMessage>> loadMessagesWithKeyword(
  1. String keywords, {
  2. String? sender,
  3. int timestamp = -1,
  4. int count = 20,
  5. MessageSearchScope searchScope = MessageSearchScope.All,
  6. ChatSearchDirection direction = ChatSearchDirection.Up,
})

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.

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 = Map();
  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 result = await ChatChannel.invokeMethod(
    ChatMethodKeys.searchChatMsgFromDB,
    req,
  );
  try {
    ChatError.hasErrorFromResult(result);
    List<ChatMessage> list = [];
    result[ChatMethodKeys.searchChatMsgFromDB]?.forEach((element) {
      list.add(ChatMessage.fromJson(element));
    });
    return list;
  } on ChatError catch (e) {
    throw e;
  }
}