addMessages method

void addMessages(
  1. List<ChatMessage> messages
)

Adds multiple messages to the chat at once.

In reverse order mode, the expected behavior with pagination is:

  • Newest messages (initial) appear at the top of the list (index 0)
  • When loading more messages, older ones appear at the bottom

In chronological order mode:

  • Oldest messages (initial) appear at the top of the list (index 0)
  • When loading more messages, newer ones appear at the bottom

Implementation

void addMessages(List<ChatMessage> messages) {
  var hasNewMessages = false;

  for (final message in messages) {
    final messageId = _getMessageId(message);
    if (!_messageCache.containsKey(messageId)) {
      // For pagination, we always append at the end regardless of order mode
      // This is appropriate for loading older messages in both modes
      _messages.add(message);
      _messageCache[messageId] = message;
      hasNewMessages = true;
    }
  }

  if (hasNewMessages) {
    notifyListeners();
  }
}