saveDataToFile static method
Implementation
static Future<SavedData> saveDataToFile(String data, String fileName) async {
try {
// Request storage permission for Android only
// if (Platform.isAndroid) {
// var status = await Permission.storage.request();
// if (!status.isGranted) {
// debugPrint('Permission denied.');
// return false;
// }
// }
//TODO:: need to check this permission block for Android 34
// Get the directory for the Downloads folder (Android) or Documents folder (iOS)
String downloadsPath = "";
if (Platform.isAndroid) {
final PackageInfo packageInfo = await PackageInfo.fromPlatform();
downloadsPath =
'/storage/emulated/0/Download/${packageInfo.appName}/transcriptions';
} else if (Platform.isIOS) {
final Directory directory = await getApplicationDocumentsDirectory();
downloadsPath = '${directory.path}/transcriptions';
}
// Ensure the directory exists
Directory downloadsFolder = Directory(downloadsPath);
if (!await downloadsFolder.exists()) {
await downloadsFolder.create(recursive: true);
}
// Create the file path
String filePath = '$downloadsPath/$fileName.txt';
File file = File(filePath);
// Write the formatted content to the file
await file.writeAsString(data);
debugPrint('File saved at $filePath');
return SavedData(isSuccess: true, filePath: filePath);
} catch (e) {
debugPrint('Error saving file: $e');
return SavedData(isSuccess: false);
}
}