Directory class abstract interface

A reference to a directory (or folder) on the file system.

A Directory is an object holding a path on which operations can be performed. The path to the directory can be absolute or relative. It allows access to the parent directory, since it is a FileSystemEntity.

The Directory also provides static access to the system's temporary file directory, systemTemp, and the ability to access and change the current directory.

Create a new Directory to give access the directory with the specified path:

var myDir = Directory('myDir');

Most instance methods of Directory exist in both synchronous and asynchronous variants, for example, create and createSync. Unless you have a specific reason for using the synchronous version of a method, prefer the asynchronous version to avoid blocking your program.

Create a directory

The following code sample creates a directory using the create method. By setting the recursive parameter to true, you can create the named directory and all its necessary parent directories, if they do not already exist.

import 'dart:io';

void main() async {
  // Creates dir/ and dir/subdir/.
  var directory = await Directory('dir/subdir').create(recursive: true);
  print(directory.path);
}

List the entries of a directory

Use the list or listSync methods to get the files and directories contained in a directory. Set recursive to true to recursively list all subdirectories. Set followLinks to true to follow symbolic links. The list method returns a Stream of FileSystemEntity objects. Listen on the stream to access each object as it is found:

import 'dart:io';

void main() async {
  // Get the system temp directory.
  var systemTempDir = Directory.systemTemp;

  // List directory contents, recursing into sub-directories,
  // but not following symbolic links.
  await for (var entity in
      systemTempDir.list(recursive: true, followLinks: false)) {
    print(entity.path);
  }
}

The use of asynchronous methods

I/O operations can block a program for some period of time while it waits for the operation to complete. To avoid this, all methods involving I/O have an asynchronous variant which returns a Future. This future completes when the I/O operation finishes. While the I/O operation is in progress, the Dart program is not blocked, and can perform other operations.

For example, the exists method, which determines whether the directory exists, returns a boolean value asynchronously using a Future.

import 'dart:io';

void main() async {
  final myDir = Directory('dir');
  var isThere = await myDir.exists();
  print(isThere ? 'exists' : 'nonexistent');
}

In addition to exists, the stat, rename, and other methods are also asynchronous.

Other resources

Implemented types
Implementers

Constructors

Directory(String path)
Creates a Directory object.
factory
Directory.fromRawPath(Uint8List path)
factory
Directory.fromUri(Uri uri)
Create a Directory from a URI.
factory

Properties

absolute Directory
A Directory whose path is the absolute path of this Directory.
no setteroverride
hashCode int
The hash code for this object.
no setterinherited
isAbsolute bool
Whether this object's path is absolute.
no setterinherited
parent Directory
The parent directory of this entity.
no setterinherited
path String
Gets the path of this directory.
no setteroverride
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
uri Uri
A Uri representing the directory's location.
no setteroverride

Methods

create({bool recursive = false}) Future<Directory>
Creates the directory if it doesn't exist.
createSync({bool recursive = false}) → void
Synchronously creates the directory if it doesn't exist.
createTemp([String? prefix]) Future<Directory>
Creates a temporary directory in this directory.
createTempSync([String? prefix]) Directory
Synchronously creates a temporary directory in this directory.
delete({bool recursive = false}) Future<FileSystemEntity>
Deletes this Directory.
override
deleteSync({bool recursive = false}) → void
Synchronously deletes this Directory.
override
exists() Future<bool>
Checks whether the file system entity with this path exists.
inherited
existsSync() bool
Synchronously checks whether the file system entity with this path exists.
inherited
list({bool recursive = false, bool followLinks = true}) Stream<FileSystemEntity>
Lists the sub-directories and files of this Directory.
listSync({bool recursive = false, bool followLinks = true}) List<FileSystemEntity>
Lists the sub-directories and files of this Directory. Optionally recurses into sub-directories.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
rename(String newPath) Future<Directory>
Renames this directory.
override
renameSync(String newPath) Directory
Synchronously renames this directory.
override
Resolves the path of a file system object relative to the current working directory.
override
resolveSymbolicLinksSync() String
Resolves the path of a file system object relative to the current working directory.
override
stat() Future<FileStat>
Calls the operating system's stat() function on path.
inherited
statSync() FileStat
Synchronously calls the operating system's stat() function on path.
inherited
toString() String
Returns a human readable representation of this Directory.
override
watch({int events = FileSystemEvent.all, bool recursive = false}) Stream<FileSystemEvent>
Start watching the FileSystemEntity for changes.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Properties

current Directory
Creates a directory object pointing to the current working directory.
getter/setter pair
systemTemp Directory
The system temp directory.
no setter