searchFuture method

Future<List> searchFuture(
  1. dynamic keyword, {
  2. List<String>? excludedPaths,
  3. dynamic filesOnly = false,
  4. dynamic dirsOnly = false,
  5. required List<String> extensions,
  6. bool reversed = false,
  7. FlutterFileUtilsSorting? sortedBy,
})

Returns a list of found items of Directory or File type or empty list. You may supply Regular Expression e.g: "*.png", instead of string.

  • filesOnly if set to true return only files
  • dirsOnly if set to true return only directories
  • You can set both to true
  • sortedBy: Sorting
  • bool reversed: in case parameter sortedBy is used
  • Example:
  • List

Implementation

Future<List<dynamic>> searchFuture(
  var keyword, {
  List<String>? excludedPaths,
  filesOnly = false,
  dirsOnly = false,
  required List<String> extensions,
  bool reversed: false,
  FlutterFileUtilsSorting? sortedBy,
}) async {
  print("Searching for: $keyword");
  // files that will be returned
  List<dynamic> founds = [];

  if (keyword.length == 0 || keyword == null) {
    throw Exception("search keyword == null");
  }

  List<Directory> dirs = await dirsTree(excludedPaths: excludedPaths);
  List<File> files =
      await filesTree(excludedPaths: excludedPaths, extensions: extensions);

  if (filesOnly == false && dirsOnly == false) {
    filesOnly = true;
    dirsOnly = true;
  }
  if (extensions.isNotEmpty) dirsOnly = false;
  // in the future fileAndDirTree will be used
  // searching in files
  if (dirsOnly == true) {
    for (var dir in dirs) {
      if (dir.absolute.path.contains(keyword)) {
        founds.add(dir);
      }
    }
  }
  // searching in files

  if (filesOnly == true) {
    for (var file in files) {
      if (file.absolute.path.contains(keyword)) {
        founds.add(file);
      }
    }
  }

  // sorting
  if (sortedBy != null) {
    return sortBy(founds, sortedBy);
  }
  return founds;
}