saveDataToDisc method
Writes data to disk.
- If
pathis provided, the data is written to that absolute path and the file name is derived from it. - Otherwise, the file is created inside filesPath using
takeThisNameas its name, or the current timestamp whentakeThisNameis null. dataTypedrives howdatais serialised:DataType.texttreats it as aString,DataType.base64decodes it and stores the decoded bytes,DataType.bytesstores it as aUint8List.- When
recursiveistrue, any missing parent directory is created; otherwise a FileSystemException is thrown if the parent directory does not exist.
Returns the file name actually used, or null when data is null /
empty.
Implementation
Future<String?> saveDataToDisc(
Object? data,
DataType dataType, {
String? takeThisName,
String? path,
bool recursive = false,
}) async {
if (data == null) return null;
if (data is String && data.isEmpty) return null;
if (data is List && data.isEmpty) return null;
String fileName;
if (path != null) {
fileName = path.split(pathJoin).last;
} else {
fileName = takeThisName ?? DateTime.now().toIso8601String();
}
if (Platform.isWindows) fileName = fileName.replaceAll(':', '');
final String resolved = validatePath(path) ??
'${await filesPath}$pathJoin$fileName';
final File file = File(resolved);
file.createSync(recursive: recursive);
switch (dataType) {
case DataType.text:
file.writeAsStringSync(data as String);
break;
case DataType.base64:
file.writeAsBytesSync(base64Decode(data as String));
break;
case DataType.bytes:
file.writeAsBytesSync(data as Uint8List);
break;
}
return fileName;
}