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

Message collection

Copy link

A MessageCollection instance allows you to swiftly create a chat view that includes all data. This page explains how to make a view using the collection.


Create a collection

Copy link

First, create a MessageCollection instance. Here, you need to set a MessageListParams instance to determine the message order and the starting point of the message list in the chat view.

final collection = MessageCollection(
    channel: channel,
    params: MessageListParams(),
    handler: MyMessageCollectionHandler(),
    startingPoint: STARTING_POINT,
);

class MyMessageCollectionHandler extends MessageCollectionHandler {
    @override
    void onMessagesAdded(MessageContext context, GroupChannel channel, List<BaseMessage> messages) {
        // Update widgets with collection.messageList.
    }
    @override
    void onMessagesUpdated(MessageContext context, GroupChannel channel, List<BaseMessage> messages) {
        // Update widgets with collection.messageList.
    }
    @override
    void onMessagesDeleted(MessageContext context, GroupChannel channel, List<BaseMessage> messages) {
        // Update widgets with collection.messageList.
    }
    @override
    void onChannelUpdated(ChannelContext context, GroupChannel channel) {
        // Update widgets with collection.messageList.
    }
    @override
    void onChannelDeleted(ChannelContext context, String deletedChannelUrl) {
        // Back to the previous page.
    }
    @override
    void onHugeGapDetected() {
        // The Chat SDK detects that more than 300 messages are missing.
        collection.dispose();        // Dispose the collection.
        createMessageCollection();   // Create a new message collection object.
        // An additional implementation is required for initialization.
    }
}

Starting point

Copy link

The reference point for message retrieval in a chat view is startingPoint. This should be specified as a timestamp.

By default, startingPoint is set to double.maxFinite.toInt(), displaying the most recent messages first. However, if the startingPoint is set to the message last read by the user, it will display messages from where the user last read.


Initialization

Copy link

After creating a MessageCollection instance, initialize the instance through the initialize() method.

try {
    // Initialize messages from the startingPoint.
    await collection.initialize();
    // Messages are retrieved through API calls from the Sendbird server.
} catch (e) {
  // Handle error.
}

Pagination

Copy link

Use the loadPrevious() and loadNext() methods to retrieve messages from the previous page and the next page.

When the loadPrevious() method is called, the Chat SDK first checks whether there're messages to load from the previous page through hasPrevious. The same goes for hasNext and loadNext().

These methods have to be called during initialization as well.

Load previous messagesLoad next messages
try {
    // Load the previous messages
    // when the scroll reaches the first message in the chat view.
    if (collection.hasPrevious) {
        await collection.loadPrevious();
    }
} catch (e) {
  // Handle error.
}

Message events

Copy link

Use MessageCollectionHandler to determine how the client app reacts to message-related events.

The following table shows possible cases where each event handler can be called.

HandlerCalled when

onMessagesAdded()

- A new message is created as a real-time event.
- New messages are fetched during changelog sync.

onMessagesUpdated()

- A message is updated as a real-time event.
- Message update is detected during changelog sync.
- The send status of a pending message changes.

onMessagesDeleted()

- A message is deleted as a real-time event.
- Message deletion is detected during changelog sync.
- The value of the MessageListParams setter such as customTypes changes.

onChannelUpdated()

- The channel information that is included in the user's current chat view is updated as a real-time event.
- Channel info update is detected during changelog sync.

onChannelDeleted()

- The current channel is deleted as a real-time event.
- Channel deletion is detected during changelog sync.
* In both cases, the entire view should be disposed of.

onHugeGapDetected()

- A huge gap is detected through background sync. In this case, you need to dispose of the view and create a new MessageCollection instance.


Huge gap

Copy link

A gap can be created for many reasons, such as when a user has been offline for days. When the client app comes back online, the Chat SDK checks with the Sendbird server to see if there are any new messages. If it detects that more than 300 messages are missing in the local cache compared to the Sendbird server, the SDK determines that there is a huge gap and onHugeGapDetected() is called.

Note: To adjust the number of missing messages that result in a call for onHugeGapDetected(), contact our support team.

In this case, the existing MessageCollection instance should be cleared through the dispose() method and a new one must be created for better performance.

collection.dispose();

Dispose of the collection

Copy link

The dispose() method should be called when you need to clear the current chat view. For example, this can be used when the current user leaves the channel.

collection.dispose();