Skip to main content
An absolute import references a Dart library using a fully qualified Uniform Resource Identifier (URI) anchored by a specific scheme. Unlike relative imports, which depend on the file system location of the importing file, absolute imports resolve resources against the Dart SDK or the project’s package configuration map.

Syntax

The syntax consists of the import directive followed by a string literal containing the URI. The URI structure is strictly defined by the scheme.
import '<scheme>:<path>';

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:
import 'dart:io';
import 'dart:async';
2. The 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’s lib/ directory.
  • Example:
// Imports 'lib/http.dart' from the 'http' package
import 'package:http/http.dart';

// Imports 'lib/utils/helper.dart' from the current package 'my_app'
import 'package:my_app/utils/helper.dart';

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