getEntityFileOnDisc<D, T> method
Loads a file whose name is stored in a column of an SQLite table.
urlColumnName: name of the column that holds the file name.key/value: condition used to locate the row (WHERE key = value, the value being bound, never pasted into the SQL).path: optional directory where the file lives. Defaults to filesPath when omitted.
The result is cast to the type parameter D:
Uint8Listto read the file as bytes.Stringto read the file as UTF-8 text.
Returns null when the row or the file cannot be found, or when D
is something other than Uint8List / String.
Implementation
Future<D?> getEntityFileOnDisc<D, T>(
String urlColumnName,
String key,
Object value, {
String? path,
}) async {
final List<String> urls = await DataAccess.instance
.getAColumnFrom<String, T>(
urlColumnName,
afterWhere: '${columnRef(key)} = ? LIMIT 1',
whereArgs: <Object?>[value],
);
if (urls.isEmpty) return null;
final String fileRef = urls.first;
final String? filePath = path != null ? '$path$pathJoin$fileRef' : null;
if (D == Uint8List) {
final Uint8List? bytes = filePath != null
? await readFileAsBytes(path: filePath)
: await readFileAsBytes(fileName: fileRef);
return bytes as D?;
}
if (D == String) {
final String? text = filePath != null
? await readFileAsString(path: filePath)
: await readFileAsString(fileName: fileRef);
return text as D?;
}
return null;
}