openFile function

dynamic openFile(
  1. Uint8List bytes,
  2. FileOpenerFileType fileType, {
  3. String baseFileName = 'temp',
  4. bool preferDownload = true,
})

Implementation

openFile(
  Uint8List bytes,
  FileOpenerFileType fileType, {
  String baseFileName = 'temp',
  bool preferDownload = true,
}) async {
  // Get temporary directory
  final tempDir = await getTemporaryDirectory();
  // Assume FileOpenerFileType has an 'extension' property, e.g., "pdf", "txt", etc.
  final filePath = '${tempDir.path}/$baseFileName.${fileType.extension}';
  final file = File(filePath);

  // Write bytes to the file
  await file.writeAsBytes(bytes, flush: true);
  // Open the file with the default app
  await OpenFile.open(file.path);
}