/ SDKs / Flutter
SDKs
Chat SDKs Flutter v4
Chat SDKs Flutter
Chat SDKs
Flutter
Version 4

Load previous messages

Copy link

Using PreviousMessageListQuery's next() method, which returns a list of BaseMessage objects, you can retrieve a set number of previous messages in a channel. With a returned list, you can display the past messages in your UI once they have loaded. The following code is an example of retrieving previous messages in a channel.

try {
  final query = PreviousMessageListQuery(
    channelType: channel.channelType,
    channelUrl: channel.channelUrl,
  )
    ..limit = 5
    ..customTypesFilter = []
    ..senderIdsFilter = []
    ..messageTypeFilter = MessageTypeFilter.all;
  final messages = await query.next();
} catch (e) {
  // Handle error.
}

PreviousMessageListQuery

Copy link

This table only contains the required properties shown in the code above. See the API reference page for a complete list of properties.

Property nameTypeDescription

channelType

String

Specifies the type of the channel.

channelUrl

String

Specifies the URL of the channel.

messageTypeFilter

MessageTypeFilter

If specified, it restricts the search scope to only retrieve messages that match the specified message type.

customTypesFilter

List<String>

If specified, it restricts the search scope to only retrieve messages that match the specified custom type.

senderIdsFilter

List<String>

If specified, it restricts the search scope to only retrieve messages sent by the users with the specified user IDs.

The limit property indicates how many messages should be included in a returned list. The PreviousMessageListQuery instance itself does the pagination of a result set according to the value of the limit property and internally manages a token to retrieve the next page in the result set.

Each time the next() method is called, the instance retrieves a set number of messages in the next page and updates the value of the token to complete the current call and prepare the next call. Before calling the next() method again, you must receive a success callback through the callback handler first.

If you create a new PreviousMessageListQuery instance and call the next() method, a set number of the most recently sent messages are retrieved because the new instance's token has nothing to do with the previously created instance. So we recommend that you create a single query instance and store it as a member variable for traversing through the entire message history.