Dart FileSystemEntityType
last modified October 18, 2026
The FileSystemEntityType class in Dart represents types of file
system entities. It's used to identify whether a path refers to a file,
directory, or link.
This enum-like class is part of Dart's dart:io library. It's
commonly used with file system operations to determine entity types before
performing specific actions.
Basic Definition
FileSystemEntityType is a final class with constant values
representing different file system entity types. It cannot be instantiated.
The common values are file, directory,
link, and notFound; unixDomainSock and
pipe are also possible. These help identify what kind of entity a
path points to in the file system.
Checking File Type
This example shows how to check if a path points to a regular file.
import 'dart:io';
void main() async {
var file = File('example.txt');
await file.writeAsString('Hello, Dart!');
var type = await FileSystemEntity.type('example.txt');
print(type == FileSystemEntityType.file); // true
await file.delete();
}
We create a file, check its type using FileSystemEntity.type(), then delete it. The method returns a FileSystemEntityType value we can compare against constants.
$ dart main.dart true
Checking Directory Type
This example demonstrates checking if a path points to a directory.
import 'dart:io';
void main() async {
var dir = Directory('test_dir');
await dir.create();
var type = await FileSystemEntity.type('test_dir');
print(type == FileSystemEntityType.directory); // true
await dir.delete();
}
We create a directory, verify its type using FileSystemEntityType.directory, then clean up. This is useful before performing directory-specific operations.
$ dart main.dart true
Checking Symbolic Link Type
This example shows how to identify a symbolic link using FileSystemEntityType.
import 'dart:io';
void main() async {
var file = File('target.txt');
await file.writeAsString('Link target');
var link = Link('link.txt');
await link.create('target.txt');
var type = await FileSystemEntity.type('link.txt', followLinks: false);
print(type == FileSystemEntityType.link); // true
await link.delete();
await file.delete();
}
We create a file and a symbolic link to it. Because type() follows
links by default, followLinks: false is required to get
FileSystemEntityType.link instead of the target's type. Always
clean up temporary files after such operations.
$ dart main.dart true
Handling Non-Existent Paths
This example demonstrates how FileSystemEntityType handles non-existent paths.
import 'dart:io';
void main() async {
var type = await FileSystemEntity.type('nonexistent.txt');
print(type == FileSystemEntityType.notFound); // true
if (type == FileSystemEntityType.notFound) {
print('Path does not exist');
}
}
When checking a non-existent path, FileSystemEntityType returns
notFound. This helps distinguish between different types of
"file not found" scenarios.
$ dart main.dart true Path does not exist
Directory Listing with Type Checking
This example shows how to list directory contents with type information.
import 'dart:io';
void main() async {
var dir = Directory('test');
await dir.create();
await File('${dir.path}/file.txt').create();
await Directory('${dir.path}/subdir').create();
var entities = dir.list();
await for (var entity in entities) {
var type = await FileSystemEntity.type(entity.path);
print('${entity.path}: ${type.toString().split('.').last}');
}
await dir.delete(recursive: true);
}
We create a directory with a file and subdirectory, then list them with types. The type() method helps categorize each entity during directory traversal.
$ dart main.dart test/file.txt: file test/subdir: directory
Best Practices
- Async handling: Always await FileSystemEntity.type() calls
- Error checking: Handle
notFoundcases appropriately - Type safety: Use == operator for type comparisons
- Cleanup: Delete temporary files after testing
Source
Dart FileSystemEntityType Documentation
This tutorial covered Dart's FileSystemEntityType class with practical examples showing how to identify different file system entities in Dart applications.
Author
List all Dart tutorials.