asPlatformFile method

Future<PlatformFile?> asPlatformFile()

Constructs the PlatformFile from this DataReader.

Implementation

Future<PlatformFile?> asPlatformFile() {
  final Completer<PlatformFile?> completer = Completer<PlatformFile?>();
  final SimpleFileFormat? format =
      getFormats(
        Formats.standardFormats,
      ).whereType<SimpleFileFormat>().lastOrNull;

  getFile(format, (DataReaderFile file) async {
    try {
      // Read the file bytes asynchronously.
      final Uint8List bytes = await file.readAll();

      final PlatformFile platformFile = PlatformFile(
        name: file.fileName ?? '',
        size: file.fileSize ?? 0,
        bytes: bytes,
      );

      completer.complete(platformFile);
    } catch (error) {
      completer.completeError(error);
    }
  });

  return completer.future;
}