fileStream function

Stream<List<FileSystemEntity>> fileStream(
  1. String path, {
  2. dynamic changeCurrentPath = true,
  3. dynamic reverse = false,
  4. dynamic recursive = false,
  5. dynamic keepHidden = false,
})

keepHidden: show files that start with .

Implementation

Stream<List<FileSystemEntity>> fileStream(String path,
    {changeCurrentPath: true,
    reverse: false,
    recursive: false,
    keepHidden: false}) async* {
  Directory _path = Directory(path);
  List<FileSystemEntity> _files = <FileSystemEntity>[];
  try {
    // Checking if the target directory contains files inside or not!
    // so that [StreamBuilder] won't emit the same old data if there are
    // no elements inside that directory.
    if (_path.listSync(recursive: recursive).length != 0) {
      if (!keepHidden) {
        yield* _path.list(recursive: recursive).transform(
            StreamTransformer.fromHandlers(
                handleData: (FileSystemEntity data, sink) {
          print("filsytem_utils -> fileStream: $data");
          _files.add(data);
          sink.add(_files);
        }));
      } else {
        yield* _path.list(recursive: recursive).transform(
            StreamTransformer.fromHandlers(
                handleData: (FileSystemEntity data, sink) {
          print("filsytem_utils -> fileStream: $data");
          if (data.basename().startsWith('.')) {
            _files.add(data);
            sink.add(_files);
          }
        }));
      }
    } else {
      yield [];
    }
  } on FileSystemException catch (e) {
    print(e);
    yield [];
  }
}