Skip to main content
The 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.
SchemeDescriptionExample
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';
RelativeReferences 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 any library declaration but preceding code definitions.
// Imports the math library from the Dart SDK
import 'dart:math';

// Imports a file named 'api.dart' from the 'network' package
import 'package:network/api.dart';

// Imports a local file located in the parent directory
import '../constants.dart';

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.
import 'package:lib1/utils.dart';
import 'package:lib2/utils.dart' as lib2;

void main() {
  // Uses Element from lib1 (imported into current scope)
  Element e1 = Element(); 
  
  // Uses Element from lib2 via the prefix
  lib2.Element e2 = lib2.Element(); 
}

Filtering (show and hide)

To control which symbols are introduced into the current library’s scope, use show (inclusion list) or hide (exclusion list).
// Only imports 'Client' and 'Response'; other symbols remain inaccessible
import 'package:http/http.dart' show Client, Response;

// Imports everything EXCEPT 'Logger'
import 'package:logging/logging.dart' hide Logger;

Deferred Loading

The deferred as keywords allow a library to be loaded lazily at runtime. The library must be loaded asynchronously using loadLibrary() before its symbols are accessed.
import 'package:heavy_lib/features.dart' deferred as features;

Future<void> loadFeatures() async {
  // Pauses execution until the library is loaded
  await features.loadLibrary();
  features.initialize();
}

Exporting

The export 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.
// In file: lib/main_api.dart

// Users importing main_api.dart gain access to symbols in auth.dart and user.dart
export 'src/auth.dart';
export 'src/user.dart' show User, UserProfile; // Selective export
Master Dart with Deep Grasping Methodology!Learn More