getDownloadPath method

Future<File> getDownloadPath({
  1. required String filename,
})

获取下载路径 filename 文件的名字,需要带上后缀(例如: eg.png)

Implementation

Future<File> getDownloadPath({required String filename}) async {
  try {
    Future<Directory> future;
    String? directory = "downloads";
    if (isAndroid()) {
      var externalCacheDirectories = await getExternalCacheDirectories();
      var externalCacheDirectory = externalCacheDirectories?.firstOrNull;
      if (externalCacheDirectory == null) {
        future = getTemporaryDirectory();
      } else {
        future = Future.value(externalCacheDirectory);
      }
    } else {
      var downloadDir = await getDownloadsDirectory();
      if (downloadDir == null) {
        future = getTemporaryDirectory();
      } else {
        directory = null;
        future = Future.value(downloadDir);
      }
    }
    var dir = await future;
    return _getFile(dir, directory, filename);
  } catch (e) {
    return File(filename);
  }
}