Dart Uri.directory()
Syntax & Examples


Uri.directory constructor

The `Uri.directory` constructor in Dart is similar to `Uri.file` but ensures that a non-empty URI path ends with a slash.


Syntax of Uri.directory

The syntax of Uri.Uri.directory constructor is:

Uri.directory(String path, { bool windows })

This Uri.directory constructor of Uri like Uri.file except that a non-empty URI path ends in a slash.

Parameters

ParameterOptional/RequiredDescription
pathrequiredThe directory path, which can be absolute or relative.
windowsoptionalA boolean flag indicating whether the path should be treated as a Windows path. Defaults to false.


✐ Examples

1 Creating a directory URI on Unix-like systems

In this example,

  1. We define a string variable `path` representing a Unix-like path.
  2. We use the `Uri.directory` constructor to create a directory URI from this path.
  3. We print the URI as a string.

Dart Program

void main() {
  String path = '/usr/local/bin';
  Uri uri = Uri.directory(path);
  print(uri.toString());
}

Output

file:///usr/local/bin/

2 Creating a directory URI on Windows with Windows conventions

In this example,

  1. We define a string variable `path` representing a Windows path.
  2. We use the `Uri.directory` constructor with the `windows` parameter set to true.
  3. We print the URI as a string.

Dart Program

void main() {
  String path = 'C:\\Program Files';
  Uri uri = Uri.directory(path, windows: true);
  print(uri.toString());
}

Output

file:///C:/Program%20Files/

Summary

In this Dart tutorial, we learned about Uri.directory constructor of Uri: the syntax and few working examples with output and detailed explanation for each example.