messages property

List<ChatMessage> messages
final

A list of ChatMessage objects that will be displayed in the chat interface.

Each message includes details such as the message text, timestamp, and author information.

{@tool snippet}

List<ChatMessage> _messages = <ChatMessage>[
  ChatMessage(
    text: 'Hello, how can I help you today?',
    time: DateTime.now(),
    author: const ChatAuthor(
      id: 'a2c4-56h8-9x01-2a3d',
      name: 'User name',
    ),
  ),
];

@override
Widget build(BuildContext context) {
  return SfChat(
    messages: _messages,
  );
}

{@end-tool}

Additionally, the message content can be extended to include more information about the chat message.

{@tool snippet}

class ChatMessageExt extends ChatMessage {
  const ChatMessageExtend({
    required super.text,
    required super.time,
    required super.author,
    required this.displayName,
    required this.aboutMessage,
  });

  final String displayName;
  final String aboutMessage;
}

List<ChatMessage> _messages = <ChatMessageExt>[
  ChatMessageExt(
    text: 'Hello, how can I help you today?',
    time: DateTime.now(),
    author: const ChatAuthor(
      id: 'a2c4-56h8-9x01-2a3d',
      name: 'User name',
    ),
    displayName: 'UN',
    aboutMessage: 'A coding enthusiast',
  ),
];

@override
Widget build(BuildContext context) {
  return SfChat(
    messages: _messages,
  );
}

{@end-tool}

Implementation

final List<ChatMessage> messages;