saveDataToDisc method
Use for saving data to a specific path on disc
- If path is not provid data will be saved in the application directory name files.
- If path (entire Lunix or windows path) is provide data will be saved in that specific path.
- If DataType (type of the data we want to save) equals DataType.text it will be saved as text file
else it is saved as binary file (e.g for image, audio,pdf,video etc.) - About recursive:
Calling saveDataToDisc on an existing file on given path or name might fail if there are restrictive permissions on the file
If recursive is false, the default, the file is created only if all directories in its path already exist.
If recursive is true, all non-existing parent paths are created first.
Throws a FileSystemException if the operation fails. - returns the name of the file or null if null or empty data was given
Implementation
Future<String?> saveDataToDisc(var data, DataType dataType,
{String? takeThisName, String? path, bool recursive = false}) async {
if (data != null && data.isNotEmpty) {
String fileName;
if (path != null) {
fileName = path.split(pathJoin).last;
} else {
fileName = takeThisName ?? DateTime.now().toString();
}
if (Platform.isWindows) fileName = fileName.replaceAll(':', '');
File fileToSave =
File(validatePath(path) ?? "${await filesPath}$pathJoin$fileName");
fileToSave.createSync(recursive: recursive);
switch (dataType) {
case DataType.text:
fileToSave.writeAsStringSync(data);
break;
case DataType.base64:
case DataType.bytes:
if (dataType == DataType.base64) data = base64Decode(data);
fileToSave.writeAsBytesSync(data);
break;
}
return fileName;
}
return null;
}