loadTree method

Future<DirectoryTree<FileSystemEntity>> loadTree(
  1. String path, {
  2. TreeOptions options = const TreeOptions(),
  3. int depth = 1,
})

Reads the directory at path and depth levels below it into a new tree built with options.

The tree holds the directory itself (a directory entry with Directory data, so with the default TreeRoots.minimal roots it is the single top-level node) and its contents down to depth levels: 1 (the default) lists its children, 2 adds their children, and 0 lists nothing. Directories on the last level are in the tree without children. Give the tree to a DirectoryTreeController whose loader is loadChildren, and those directories load when they are expanded. An empty directory looks the same as one that was not read yet, so the controller lists it once more when it is first expanded.

path is read as given, so a relative path gives a relative tree, whose later loads resolve against the current directory of that moment. A relative path that climbs above its start, such as .. or ../project, is read through its absolute form instead, as DirectoryScanner.scanTree does. On Linux and macOS, also as the scanner does, a leading // reads as /, and a relative path whose first segment looks like a drive (c:, c:/x) reads as ./c:, so the tree stays relative. On Windows, pass C:\ for the root of a drive: dart:io reads C: alone as the current directory of that drive, while the tree would call it C:/.

Entry paths are parsed by TreeOptions.paths with PathPolicy.backslashIsSeparator set to the host rule (Platform.isWindows) whatever options says, as DirectoryScanner.scanTree does: dart:io reports host paths, and on Linux and macOS \ is an ordinary character in names (x\y.txt is one file). Every other setting of options is used as given, and the returned tree's DirectoryTree.options show the policy actually used. A tree built another way for loadChildren should follow the same rule.

Completes with an ArgumentError when depth is negative, a PathNotFoundException when nothing exists at path, a FileSystemException when path is not a directory or cannot be listed completely (for example a PathAccessException without read permission), and any exception skip throws.

A directory below path that cannot be listed does not fail the call: it stays in the tree without children, so the controller tries again when it is expanded and reports the failure through its onLoadError. To be told about every failure right away, scan with a DirectoryScanner and its onError instead.

Reads one directory at a time with asynchronous dart:io calls, so the UI stays responsive. Takes one directory listing per directory above the last level, and O(n log n) time for the n entities read.

Implementation

Future<DirectoryTree<FileSystemEntity>> loadTree(
  String path, {
  TreeOptions options = const TreeOptions(),
  int depth = 1,
}) async {
  if (depth < 0) {
    throw ArgumentError.value(depth, 'depth', 'must be >= 0');
  }
  final effective = _hostOptions(options);
  final policy = effective.paths;
  final readPath = _readPath(path, policy);
  final base = policy.tryParse(readPath)?.toString() ?? readPath;
  final List<TreeEntry<FileSystemEntity>> children;
  if (depth == 0) {
    // The scanner checks that readPath is a directory, with the same
    // errors as a listing, without listing it.
    await DirectoryScanner(
      followLinks: followLinks,
      maxDepth: 0,
    ).scan(readPath);
    children = const <TreeEntry<FileSystemEntity>>[];
  } else {
    children = await _list(readPath, base);
  }
  final entries = <TreeEntry<FileSystemEntity>>[
    TreeEntry<FileSystemEntity>.directory(base, data: Directory(readPath)),
    ...children,
  ];
  if (depth > 1) await _listBelow(readPath, children, depth, entries);
  return DirectoryTree<FileSystemEntity>.build(entries, options: effective);
}