loadMessages method

Future<List<EMMessage>> loadMessages({
  1. String startMsgId = '',
  2. int loadCount = 20,
  3. EMSearchDirection direction = EMSearchDirection.Up,
})

~english Loads multiple messages from the local database.

Loads messages from the local database before the specified message.

The loaded messages will also join the existing messages of the conversation stored in the memory.

Param startMsgId The starting message ID. Message loaded in the memory before this message ID will be loaded. If the startMsgId is set as "" or null, the SDK will first load the latest messages in the database.

Param loadCount The number of messages per page.

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 从本地数据库加载消息。

根据传入的参数从本地数据库加载 startMsgId 之前(存储顺序)指定数量的消息。

Param startMsgId 加载这个 ID 之前的 message,如果传入 "" 或者 null,将从最近的消息开始加载。

Param loadCount 加载的条数。

Param direction 消息的加载方向。

Return 消息列表。

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

Implementation

Future<List<EMMessage>> loadMessages({
  String startMsgId = '',
  int loadCount = 20,
  EMSearchDirection direction = EMSearchDirection.Up,
}) async {
  Map req = this._toJson();
  req["startId"] = startMsgId;
  req['count'] = loadCount;
  req['direction'] = direction == EMSearchDirection.Up ? "up" : "down";

  Map<String, dynamic> result = await _emConversationChannel.invokeMethod(
      ChatMethodKeys.loadMsgWithStartId, req);

  try {
    EMError.hasErrorFromResult(result);
    List<EMMessage> msgList = [];
    result[ChatMethodKeys.loadMsgWithStartId]?.forEach((element) {
      msgList.add(EMMessage.fromJson(element));
    });
    return msgList;
  } on EMError catch (e) {
    throw e;
  }
}