I need to determine if a path is folder or a file using Dart.
The FileSystem class has methods: isDirectory and isFile.
I don't understand how to access FileSystem class and these class methods.
Could you please provide your suggestions.
Thank You
You can try something like that:
import 'dart:io';
Future<String> isFileOrDirectory(String path) async {
try {
final isDirectory = await Directory(path).exists();
final isFile = await File(path).exists();
if (isDirectory) {
return 'directory';
} else if (isFile) {
return 'file';
} else {
return 'error';
}
} catch (e) {
return 'error';
}
}
void main() async {
String result = await isFileOrDirectory('./mydirectory');
print(result);
}
Feel free to adapt this code to your needs
===EDIT===
According to @pskink, you can also use this:
Future<String> isFileOrDirectory(String path) async {
FileSystemEntityType type = await FileSystemEntity.type(path);
return type.toString();
}
void main() async {
String result = await isFileOrDirectory('./mydirectory');
print(result);
}