loadAsXFile method

Future<XFile?> loadAsXFile({
  1. String? fileName,
})

Loads the asset image at this String path into a temporary XFile.

Optionally override the fileName used in temp directory. Returns null if loading fails.

Implementation

Future<XFile?> loadAsXFile({String? fileName}) async {
  try {
    final ByteData byteData = await rootBundle.load(this);
    final Uint8List bytes = byteData.buffer.asUint8List();

    final Directory tempDir = await getTemporaryDirectory();
    final String name = fileName ?? split('/').last;
    final String filePath = '${tempDir.path}/$name';

    final File file = File(filePath);
    await file.writeAsBytes(bytes, flush: true);

    return XFile(file.path);
  } catch (e, stack) {
    debugPrint('AssetToXFileExtension | loadAsXFile | Error: $e');
    debugPrintStack(stackTrace: stack);
    return null;
  }
}