loadMessages method

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

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.

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;
  }
}