getSafePath function

Future<String?> getSafePath(
  1. List<String> subFolders
)

Return string - new path with only safe characters in DocumentsDirectory.

It join all subFolders into path string and make safety checks. If file plugin or DocumentsDirectory didn't exist - return null.

Implementation

Future<String?> getSafePath(List<String> subFolders) async {
  if (kIsWeb) {
    return '';
  }

  Directory? appDocDir;
  try {
    appDocDir = Directory(path.join(
      (await getApplicationDocumentsDirectory()).path,
      'Ais3uson',
    ));
    if (!appDocDir.existsSync()) {
      appDocDir.createSync(recursive: true);
    }
  } on MissingPlatformDirectoryException {
    log.severe('Can not find folder');
  } on MissingPluginException {
    log.severe('Can not find plugin');
  }
  appDocDir ??= await getApplicationSupportDirectory();
  //
  // > create path without special characters
  //

  return [
    appDocDir.path,
    ...subFolders.map(safeName),
  ].reduce(path.join);
}