addAttachment method
Adds a new attachment to the message.
Implementation
Future<void> addAttachment(Attachment attachment) async {
assert(attachment.fileSize != null, '');
if (attachment.fileSize! > maxAttachmentSize) {
throw ArgumentError(
'The size of the attachment is ${attachment.fileSize} bytes, '
'but the maximum size allowed is $maxAttachmentSize bytes.',
);
}
final file = attachment.file;
final uploadState = attachment.uploadState;
// No need to cache the attachment if it's already uploaded
// or we are on web.
if (file == null || uploadState.isSuccess || isWeb) {
value = [...value, attachment];
return;
}
// Cache the attachment in a temporary file.
final tempFilePath = await _saveToCache(file);
value = [
...value,
attachment.copyWith(
file: file.copyWith(
path: tempFilePath,
),
),
];
}