getFile method

Future<File> getFile(
  1. String objectType,
  2. dynamic objectId,
  3. String fileName, {
  4. String? saveToDirectory,
})

Implementation

Future<File> getFile(String objectType, dynamic objectId, String fileName, {String? saveToDirectory}) async {
  return await apiGetWithTokenUrl("/api/$objectType/$objectId/$fileName").then((fileRes) async {
    debug("Processing file:");
    File file;
    if (saveToDirectory != null) {
      file = File(saveToDirectory + "/$fileName");
    } else {
      var dir = await getTemporaryDirectory();
      var itemDir = Directory("${dir.path}/vasat/$objectType");
      if (!itemDir.existsSync()) {
        // Copy file to app directory location
        itemDir.createSync();
      }
      file = File("${itemDir.path}/$fileName");
    }
    file.writeAsBytesSync(fileRes, flush: true);
    debug("Processing file written");
    return file;
  });
}