recListFile function
Recursively browses the contents of a directory and returns type File.
If there are not enough permissions to view the directory, then the directory is skipped.
This method solves problem of closing stream during recursive traversal (https://github.com/dart-lang/sdk/issues/54803).
exmaple:
dir
- subDir1
- file_b
- file_c
- subDir2
- rootDir <- PathAccessException: Directory listing failed, path = 'dir/rootDir' (OS Error: Permission denied, errno = 13)
- file2
- file2
dir.list(recursive: ture) - closes the stream with an error PathAccessException.
recListFile(dir) - skips content in derictory dir/rootDir to avoid closing the stream.
Implementation
Stream<File> recListFile(
Directory dir,
) async* {
try {
await for (final entitie in dir.list()) {
switch (entitie.statSync().type) {
case FileSystemEntityType.file:
yield File(entitie.path);
break;
case FileSystemEntityType.directory:
yield* recListFile(Directory(entitie.path));
break;
default:
break;
}
}
} on PathAccessException {
print("Insufficient permissions to read subdirectories :( (${dir.path})");
}
}