delete function
Delete a file from the file system.
This function removes a single file. For directories, use deleteDir.
Parameters:
path: The file path to delete
Throws:
- Exception if the file doesn't exist
- Exception if the path is a directory
- Exception if there are insufficient permissions
Example:
delete('/path/to/file.txt');
Implementation
void delete(String path) {
// Validate input
if (path.isEmpty) {
StatusHelper.failed('File path cannot be empty.');
}
final absolutePath = truepath(path);
if (!exists(absolutePath)) {
StatusHelper.failed('The file $absolutePath does not exist.');
}
if (isDirectory(absolutePath)) {
StatusHelper.failed(
'The path $absolutePath is a directory. Use deleteDir for directories.');
}
try {
File(absolutePath).deleteSync();
} on FileSystemException catch (e) {
StatusHelper.failed('Failed to delete file $absolutePath: ${e.message}');
} catch (e) {
StatusHelper.failed(
'An unexpected error occurred deleting $absolutePath: $e');
}
}