createFolderByPath function

Future<Directory> createFolderByPath(
  1. String path, {
  2. String? folderName,
})

Create folder by path

  • i.e: .createFolderByPath("/storage/emulated/0/", "folder name" )

Supply path alone to create by already combined path, or path + filename to be combined

Implementation

Future<Directory> createFolderByPath(String path, {String? folderName}) async {
  print("filesystem_utils->createFolderByPath: $folderName @ $path");
  var _directory;

  if (folderName != null) {
    _directory = Directory(pathlib.join(path, folderName));
  } else {
    _directory = Directory(path);
  }

  try {
    if (!_directory.existsSync()) {
      _directory.create();
    } else {
      FileSystemException("File already exists");
    }
    return _directory;
  } catch (e) {
    throw FileSystemException(e as String);
  }
}