ChatMessage.fromJson constructor
Converts a JSON map representation to a ChatMessage.
The map should contain the following keys:
- 'origin': The origin of the message (user or model).
- 'text': The text content of the message.
- 'attachments': A list of attachments, each represented as a map with:
- 'type': The type of the attachment ('file' or 'link').
- 'name': The name of the attachment.
- 'mimeType': The MIME type of the attachment.
- 'data': The data of the attachment, either as a base64 encoded string (for files) or a URL (for links).
Implementation
factory ChatMessage.fromJson(Map<String, dynamic> map) => ChatMessage(
origin: MessageOrigin.values.byName(map['origin'] as String),
text: map['text'] as String,
thinking: map['thinking'] as String?,
attachments: [
for (final attachment in map['attachments'] as List<dynamic>)
switch (attachment['type'] as String) {
'file' => FileAttachment.fileOrImage(
name: attachment['name'] as String,
mimeType: attachment['mimeType'] as String,
bytes: base64Decode(attachment['data'] as String),
),
'link' => LinkAttachment(
name: attachment['name'] as String,
url: Uri.parse(attachment['data'] as String),
),
_ => throw UnimplementedError(),
},
],
);