Skip to main content
A relative import is a directive that references a Dart library source file using a URI path relative to the base URI of the importing file. Unlike package: imports, which utilize a logical namespace defined by the package configuration, relative imports resolve locations based on the directory hierarchy using standard URI path resolution rules.

Syntax and Path Resolution

Relative imports are defined using string literals containing a relative URI reference. The Dart compiler resolves these paths by merging the relative string with the URI of the current library.
ComponentDescription
filename.dartReferences a file located in the same directory as the current file.
./Explicitly references the current directory.
../Traverses to the parent directory.
../../Traverses to the grandparent directory (recursive traversal).

Directory Structure Example

The following project structure is used for the implementation examples:
my_project/
├── lib/
│   ├── main.dart
│   ├── models/
│   │   └── user.dart
│   └── utils/
│       ├── date_formatter.dart
│       └── string_helper.dart

Implementation Examples

1. Importing a sibling file In lib/utils/date_formatter.dart, importing a file in the same directory:
// Resolves to lib/utils/string_helper.dart
import 'string_helper.dart'; 

// Functionally equivalent to the above
import './string_helper.dart';
2. Importing a file from a subdirectory In lib/main.dart, importing a file located deeper in the directory tree:
// Resolves to lib/models/user.dart
import 'models/user.dart';
3. Importing a file from a parent directory In lib/models/user.dart, traversing up to the root of lib:
// Resolves to lib/main.dart
import '../main.dart';
4. Importing across sibling directories In lib/models/user.dart, traversing up to lib and then down into utils:
// Resolves to lib/utils/date_formatter.dart
import '../utils/date_formatter.dart';

Library Identity and URI Resolution

Dart identifies libraries uniquely by their resolved URI. A relative import inherits the URI scheme (e.g., package: or file:) of the importing library. If a file is imported via a package: URI in one context and a relative path that resolves to a file: URI in another, the Dart runtime treats them as two distinct libraries. This often occurs when importing lib files relatively from outside the lib directory (such as from test/ or bin/). Example of Library Duplication:
  1. Context A: main.dart imports models/user.dart.
    • Resolved URI: package:my_project/models/user.dart
  2. Context B: test/user_test.dart imports ../lib/models/user.dart.
    • Resolved URI: file:///path/to/my_project/lib/models/user.dart
Because the URIs differ, the runtime creates two separate instances of the library. Consequently, a User object created in Context A is not compatible with the User type expected in Context B, resulting in type mismatch errors (e.g., “User is not of type User”). To maintain type consistency, imports should consistently resolve to the same URI scheme.
Master Dart with Deep Grasping Methodology!Learn More