loadMessages method Null safety
- {String startMsgId = '',
- int loadCount = 20,
- EMSearchDirection direction = EMSearchDirection.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. The {@link #getAllMessages()} method returns all messages of the conversation loaded 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 {@link EMError}.
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;
}
}