Is there a better way to find out of a file or directory exists?

Issue

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?

Solution

A shorter way is

import 'dart:io';

FileSystemEntity.typeSync(path) != FileSystemEntityType.notFound

See also https://github.com/dart-lang/sdk/issues/2883#issuecomment-108317456

Answered By – Günter Zöchbauer

Answer Checked By – Timothy Miller (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *