0

Problem

I get the FileSystemEntity list and want to select only Directories from it. So I create a new list and remove all entity types from it except Directory. However, the list just happens to be empty (it is guaranteed that subdirectories exist).

final inputDir = Directory(path); // home

final onlyDirs = await inputDir.list().toList(); // get subdirs
onlyDirs.removeWhere((element) => element.runtimeType != Directory); // remove all except Directory

My solution

I was trying to check element.runtimeType is! Directory, but it does not work at all. Debugger says that element.runtimeType is _Directory, but obviously I can't use private class in my comparator.

Question

How to remove all entities except directories from the List<FileSystemEntity>?

L3odr0id
  • 170
  • 1
  • 10
  • 2
    *"How to check if FileSystemEntity is a Directory"* - `final onlyDirs = await inputDir.list().where((fse) => fse is Directory).toList();` – pskink Mar 10 '22 at 18:31
  • Yeah, thanks a lot. But linter suggested to use `whereType()` So it goes `final onlyDirs = inputDir.whereType().toList();` – L3odr0id Mar 11 '22 at 09:17
  • 1
    you cannot call `whereType` on `inputDir` - `inputDir` is `Directory` object, not `Iterable` - you would need to create a `List` with files and folders first and then another filtered `List` that contains only folders – pskink Mar 11 '22 at 09:55
  • 1
    Yes, what I meant was that using `whereType<>` is visually more convenient than checking by a lambda. The problem is solved anyway. – L3odr0id Mar 11 '22 at 11:11

0 Answers0