saveBytes method

Future<Map> saveBytes(
  1. Uint8List bytes,
  2. String fileName,
  3. String fileExtension
)

Implementation

Future<Map> saveBytes(Uint8List bytes, String fileName, String fileExtension) async {
  try {
    // Verificar permisos de almacenamiento
    if (!await requestStoragePermission()) throw 'Permiso de almacenamiento no otorgado.';

    // Obtener la carpeta de Documentos
    final directory = Directory('/storage/emulated/0/Documents');
    if (!directory.existsSync()) throw 'Carpeta de Documentos no encontrada.';

    final filePath = '${directory.path}/$fileName.$fileExtension';
    final file = File(filePath);
    await file.writeAsBytes(bytes);

    String message = 'File saved successfuly: $filePath';
    return {'type': SnackType.success, 'message' : message, 'path': filePath};
  } catch (e) {
    String message = 'File save error: ${e.toString()}';
    return {'type': SnackType.error, 'message' : message};
  }
}