import directive makes public symbols (classes, functions, and variables) declared in an external library available within the current library. It utilizes a Uniform Resource Identifier (URI) to locate the target resource and resolve its declarations into the current library’s scope.
URI Schemes
Dart resolves imports based on the URI scheme provided in the string literal.| Scheme | Description | Example |
|---|---|---|
dart: | References built-in libraries provided by the Dart SDK. | import 'dart:io'; |
package: | References files within the lib/ directory of a package (including the current package) as resolved by the package manager. | import 'package:utils/calc.dart'; |
| Relative | References files relative to the current file’s location in the file system. | import '../models/user.dart'; |
Basic Syntax
The directive must appear at the top of the Dart file, following anylibrary declaration but preceding code definitions.
Namespace Management
By default,import introduces all public symbols from the referenced library into the current library’s namespace. Dart libraries are encapsulated; importing a file does not add symbols to a global application-wide scope, but rather makes them visible only within the specific library declaring the import.
Prefixing (as)
The as keyword binds the imported library to a named identifier. This prevents naming collisions by requiring symbols to be accessed using dot notation on the prefix.
Filtering (show and hide)
To control which symbols are introduced into the current library’s scope, use show (inclusion list) or hide (exclusion list).
Deferred Loading
Thedeferred as keywords allow a library to be loaded lazily at runtime. The library must be loaded asynchronously using loadLibrary() before its symbols are accessed.
Exporting
Theexport directive exposes symbols from another library through the current library’s public API. It does not import those symbols into the current library’s scope; it only makes them available to consumers of the current library.
Master Dart with Deep Grasping Methodology!Learn More





