searchFuture method
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 totrue
return only filesdirsOnly
if set totrue
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;
}