Directory class

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 intance 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 asynchronnous 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' : 'non-existent');
}

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

Other resources

Implemented types

Constructors

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

Properties

absoluteDirectory
read-only, override
A Directory whose path is the absolute path of this. [...]
hashCodeint
read-only, inherited
The hash code for this object. [...]
isAbsolutebool
read-only, inherited
Whether this object's path is absolute. [...]
parentDirectory
read-only, inherited
The parent directory of this entity.
pathString
read-only, override
Gets the path of this directory.
runtimeTypeType
read-only, inherited
A representation of the runtime type of the object.
uriUri
read-only, override
A Uri representing the directory's location. [...]

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>
inherited
Deletes this FileSystemEntity. [...]
deleteSync({bool recursive = false}) → void
inherited
Synchronously deletes this FileSystemEntity. [...]
exists() → Future<bool>
inherited
Checks whether the file system entity with this path exists. [...]
existsSync() → bool
inherited
Synchronously checks whether the file system entity with this path exists. [...]
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
inherited
Invoked when a non-existent method or property is accessed. [...]
rename(String newPath) → Future<Directory>
override
Renames this directory. [...]
renameSync(String newPath) → Directory
override
Synchronously renames this directory. [...]
Resolves the path of a file system object relative to the current working directory. [...]
resolveSymbolicLinksSync() → String
override
Resolves the path of a file system object relative to the current working directory. [...]
stat() → Future<FileStat>
inherited
Calls the operating system's stat() function on path. [...]
statSync() → FileStat
inherited
Synchronously calls the operating system's stat() function on path. [...]
toString() → String
override
Returns a human readable representation of this Directory.
watch({int events = FileSystemEvent.all, bool recursive = false}) → Stream<FileSystemEvent>
inherited
Start watching the FileSystemEntity for changes. [...]

Operators

operator ==(Object other) → bool
inherited
The equality operator. [...]

Static Properties

currentDirectory
read / write
Creates a directory object pointing to the current working directory.
systemTempDirectory
read-only
The system temp directory. [...]

© 2012 the Dart project authors
Licensed under the Creative Commons Attribution-ShareAlike License v4.0.
https://api.dart.dev/stable/2.13.0/dart-io/Directory-class.html