This seems a bit cumbersome
var fileExists =
new File(path).existsSync() ||
new Directory(path).existsSync() ||
new Link(path).existsSync()
Is there a shorter or better way?
To check if the path you have is a file or directory, import os module and use isfile() method to check if it is a file, and isdir() method to check if it is a directory.
File. exists() is used to check whether a file or a directory exists or not. This method returns true if the file or directory specified by the abstract path name exists and false if it does not exist.
To check if a file exists, you pass the file path to the exists() function from the os. path standard library. If the file exists, the exists() function returns True .
The isDir() function is used to check a given file is a directory or not.
A shorter way is
import 'dart:io';
FileSystemEntity.typeSync(path) != FileSystemEntityType.notFound
See also https://github.com/dart-lang/sdk/issues/2883#issuecomment-108317456
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With