loadMessages method

Future<List<ChatMessage>> loadMessages({
  1. String startMsgId = '',
  2. int loadCount = 20,
  3. ChatSearchDirection direction = ChatSearchDirection.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: 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. ~end

~chinese 从本地数据库加载消息。

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

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

Param loadCount 加载的条数。

Param direction 消息的加载方向。

Return 消息列表。

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

Implementation

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

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

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