getEntityFileOnDisc<D, T> method

Future<D?> getEntityFileOnDisc<D, T>(
  1. String urlColumnName,
  2. String key,
  3. Object value, {
  4. String? path,
})

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).
  • path: optional directory where the file lives. Defaults to filesPath when omitted.

The result is cast to the type parameter D:

  • Uint8List to read the file as bytes.
  • String to 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: "$key = '$value' LIMIT 1",
  );
  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;
}