fromFile static method

Future<ImageFileAttachment> fromFile(
  1. XFile file
)
override

Creates an ImageFileAttachment from an XFile.

This factory method asynchronously reads the file content and determines its MIME type. It throws an exception if the file is not an image.

file is the XFile object representing the image file to be attached.

Returns a Future that completes with an ImageFileAttachment instance. Throws an Exception if the file is not an image.

Implementation

static Future<ImageFileAttachment> fromFile(XFile file) async {
  final mimeType = Attachment._mimeType(file);
  if (!Attachment._isImage(mimeType)) {
    throw Exception('Not an image: $mimeType');
  }

  return ImageFileAttachment(
    name: file.name,
    mimeType: mimeType,
    bytes: await file.readAsBytes(),
  );
}