/ SDKs / Flutter
SDKs
Chat SDKs Flutter v3
Chat SDKs Flutter
Chat SDKs
Flutter
Version 3
Sendbird Chat SDK v3 for Flutter is no longer supported as a new version is released. Check out our latest Chat SDK v4

Load previous messages

Copy link

Using PreviousMessageListQuery's loadNext() 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.

final query = PreviousMessageListQuery(
        channelType: channel.channelType,
        channelUrl: channel.channelUrl,
      );
        ..limit = 5
        ..customTypesFilter = []
        ..senderIdsFilter = []
        ..messageTypeFilter = MessageTypeFilter.all;
      final messages = await query.loadNext();

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

Array of strings

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

senderIdsFilter

Array of strings

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 loadNext() 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 loadNext() method again, you must receive a success callback through the callback handler first.

If you create a new PreviousMessageListQuery instance and call the loadNext() 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.