Reputation: 11
I'm new to this dart stuff and having problems with creating a list directory.tell me how to handle the exception so that the loop continues
void main() async{
Directory baseDir=new Directory('l:\\');
print(baseDir.path);
try {
await for (var entity in baseDir.list(recursive: true, followLinks: false)){
print(entity);
}
}
catch(e) {
print("Error: $e");
}
}
Error: FileSystemException: Directory listing failed, path = 'l:$RECYCLE.BIN\S-1-5-18*' (OS Error: access denied. , errno = 5)
catch(e) {
print("Error: $e");
continue;
}
does not work because it is not in the loop body
Upvotes: 0
Views: 382
Reputation: 90015
You should be able to use Stream.handleError
to swallow FileSystemException
s:
import 'dart:io';
void main() async {
var baseDir = Directory.current;
var stream = baseDir
.list(
recursive: true,
followLinks: false,
)
.handleError(
(e) => print('Ignoring error: $e'),
test: (e) => e is FileSystemException,
);
await for (var entity in stream) {
print(entity);
}
print('Completed successfully.');
}
I don't have a Windows machine handy with a Dart development environment, but trying to simulate an equivalent situation as yours on my Linux machine:
$ mkdir someSubdir
$ chmod ugo-rx someSubdir # Remove permissions to list or enter the directory.
$ dart dirlist.dart
Directory: '/tmp/dirlist/someSubdir'
Ignoring error: FileSystemException: Directory listing failed, path = '/tmp/dirlist/someSubdir' (OS Error: Permission denied, errno = 13)
File: '/tmp/dirlist/dirlist.dart'
Completed successfully.
Also see: https://dart.dev/tutorials/language/streams#modify-stream-methods
Upvotes: 1