loadMessagesWithMsgType method

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

~english 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: EMSearchDirection.

  • EMSearchDirection.Up: Messages are retrieved in the reverse chronological order of when the server received messages.
  • EMSearchDirection.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 EMError. ~end

~chinese 根据消息类型、搜索消息的时间点、搜索结果的最大条数、搜索来源和搜索方向从 SDK 本地数据库中搜索指定数量的消息。

Note 当 maxCount 非常大时,需要考虑内存消耗。

Param type 消息类型,文本、图片、语音等等。

Param timestamp 搜索时间。

Param count 搜索结果的最大条数。

Param sender 搜索来自某人的消息,也可用于搜索群组或聊天室里的消息。

Param direction 消息加载的方向。

Return 消息列表。

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

Implementation

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