loadMessagesWithMsgType method

Future<List<ChatMessage>> loadMessagesWithMsgType(
  1. {required MessageType type,
  2. int timestamp = -1,
  3. int count = 20,
  4. String? sender,
  5. ChatSearchDirection direction = ChatSearchDirection.Up}
)

Retrieves messages from the database according to the following parameters: the message type, the Unix timestamp, max count, sender.

Note Be cautious about the memory usage when the maxCount is large.

Param type The message type, including TXT, VOICE, IMAGE, and so on.

Param timestamp The Unix timestamp for the search.

Param count The max number of messages to search.

Param sender The sender of the message. The param can also be used to search in group chat or chat room.

Param direction The direction in which the message is loaded: ChatSearchDirection.

  • ChatSearchDirection.Up: Messages are retrieved in the reverse chronological order of when the server received messages.
  • ChatSearchDirection.Down: Messages are retrieved in the chronological order of when the server received messages.

Return The message list.

Throws A description of the exception. See ChatError.

Implementation

Future<List<ChatMessage>> loadMessagesWithMsgType({
  required MessageType type,
  int timestamp = -1,
  int count = 20,
  String? sender,
  ChatSearchDirection direction = ChatSearchDirection.Up,
}) async {
  Map req = this._toJson();
  req['msgType'] = messageTypeToTypeStr(type);
  req['timestamp'] = timestamp;
  req['count'] = count;
  req['direction'] = direction == ChatSearchDirection.Up ? "up" : "down";
  req.putIfNotNull("sender", sender);
  Map result = await _emConversationChannel.invokeMethod(
      ChatMethodKeys.loadMsgWithMsgType, req);
  try {
    ChatError.hasErrorFromResult(result);
    List<ChatMessage> list = [];
    result[ChatMethodKeys.loadMsgWithMsgType]?.forEach((element) {
      list.add(ChatMessage.fromJson(element));
    });
    return list;
  } on ChatError catch (e) {
    throw e;
  }
}