Syntax
The syntax consists of theimport directive followed by a string literal containing the URI. The URI structure is strictly defined by the scheme.
Supported Schemes
Dart utilizes two primary schemes for absolute imports:dart: and package:.
1. The dart: Scheme
This scheme references libraries built directly into the Dart SDK.
- Structure:
dart:<library_identifier> - Resolution: The compiler resolves these URIs internally against the SDK’s platform libraries.
- Example:
package: Scheme
This scheme references libraries located in the lib/ directory of a package. This applies to external dependencies defined in pubspec.yaml as well as the current project’s own files.
- Structure:
package:<package_name>/<path_to_file> - Resolution: The compiler utilizes the package configuration file (typically
.dart_tool/package_config.json) to map the<package_name>to a specific root directory on the file system. The path segment following the package name is resolved relative to that package’slib/directory. - Example:
Library Identity
In Dart, a library is uniquely identified by the URI used to load it. This URI serves as the canonical namespace for the library’s types (classes, enums, mixins) and top-level static state (variables). URI Consistency Because absolute imports rely on fixed schemes (dart: or package:) rather than relative file paths, they provide a stable identity for a library regardless of where the import directive is declared.
Distinct Identities
If a single file is loaded via two different URI schemes (for example, once via package:my_app/a.dart and theoretically via file:///path/to/my_app/lib/a.dart), the Dart runtime treats them as two distinct libraries. The static state is duplicated, and types defined in one instance are not compatible with types defined in the other. Adhering to the package: scheme for all source files within the lib/ directory ensures a singular, canonical identity.
Master Dart with Deep Grasping Methodology!Learn More





