filesTree method

Future<List<File>> filesTree({
  1. List<String>? extensions,
  2. List<String>? excludedPaths,
  3. dynamic excludeHidden = false,
  4. bool reversed = false,
  5. FlutterFileUtilsSorting? sortedBy,
})

Return tree List of files starting from the root of type File

  • excludedPaths example: '/storage/emulated/0/Android' no files will be returned from this path, and its sub directories
  • sortedBy: Sorting
  • bool reversed: in case parameter sortedBy is used

Implementation

Future<List<File>> filesTree(
    {List<String>? extensions,
    List<String>? excludedPaths,
    excludeHidden = false,
    bool reversed: false,
    FlutterFileUtilsSorting? sortedBy}) async {
  List<File> files = [];

  List<Directory> dirs = await dirsTree(
      excludedPaths: excludedPaths, excludeHidden: excludeHidden);

  dirs.insert(0, Directory(root.path));

  if (extensions != null) {
    for (var dir in dirs) {
      for (var file
          in await listFiles(dir.absolute.path, extensions: extensions)) {
        if (excludeHidden) {
          if (!file.path.startsWith("."))
            files.add(file);
          else
            print("Excluded: ${file.path}");
        } else {
          files.add(file);
        }
      }
    }
  } else {
    for (var dir in dirs) {
      for (var file in await listFiles(dir.absolute.path)) {
        if (excludeHidden) {
          if (!file.path.startsWith("."))
            files.add(file);
          else
            print("Excluded: ${file.path}");
        } else {
          files.add(file);
        }
      }
    }
  }

  if (sortedBy != null) {
    return sortBy(files, sortedBy) as FutureOr<List<File>>;
  }

  return files;
}