getDownloadFolderPath method

Future<String?>? getDownloadFolderPath()

This tell user about where is download directory located in flutter

Implementation

Future<String?>? getDownloadFolderPath() async {
  Directory? directory;

  try {
    if (Platform.isAndroid) {
      if (await requestPermission(Permission.storage)) {
        directory = (await pp.getExternalStorageDirectory())!;
        String newPath = "";
        print(directory);
        List<String> paths = directory.path.split("/");
        for (int x = 1; x < paths.length; x++) {
          String folder = paths[x];
          if (folder != "Android") {
            newPath += "/" + folder;
          } else {
            break;
          }
        }
        newPath = newPath + "/Download";
        directory = Directory(newPath);
      } else {
        return "";
      }
    } else {
      if (await requestPermission(Permission.photos)) {
        directory = await pp.getApplicationDocumentsDirectory();
      } else {
        return "";
      }
    }

    if (!await directory.exists()) {
      await directory.create(recursive: true);
    }
    if (await directory.exists()) {
      return directory.path;
    }
  } catch (e) {
    print(e);
  }
  if (directory == null) return null;

  return directory.path;
}