Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The import directive in Dart is a compilation mechanism used to bring the declarations (classes, functions, variables) of an external or internal library into the lexical scope of the current library. It relies on Uniform Resource Identifiers (URIs) to resolve and link dependencies during the compilation or interpretation phase. Dart resolves imports using URIs, which generally fall into three categories based on the presence or absence of a URI scheme:
  1. dart: scheme: Resolves to built-in Dart SDK libraries.
  2. package: scheme: Resolves to external dependencies managed by the Dart package manager (pub) and declared in the pubspec.yaml file, as well as absolute imports within the current package’s lib directory.
  3. Relative URIs (No scheme): Resolves to local files relative to the importing file’s directory structure using standard relative path notation.
// Core SDK import (dart: scheme)
import 'dart:async';

// Package dependency import (package: scheme)
import 'package:yaml/yaml.dart';

// Relative local import (no scheme)
import '../models/user_model.dart';

Namespace Prefixing

To prevent namespace collisions when multiple libraries expose identical top-level symbols, Dart provides the as keyword. This assigns a specific identifier (prefix) to the imported library, requiring all members of that library to be accessed through the prefix.
import 'package:http/http.dart' as http;

// Symbols are now scoped to the 'http' prefix
// http.Client()

Combinators (Visibility Control)

Dart allows granular control over which symbols are introduced into the current namespace using combinators.
  • show (Allowlist): Imports only the specified symbols, ignoring the rest of the library.
  • hide (Blocklist): Imports the entire library except the specified symbols.
// Imports only 'max' and 'pi'
import 'dart:math' show max, pi;

// Imports everything except 'Random'
import 'dart:math' hide Random;

Deferred Loading

The deferred as directive instructs the Dart compiler to load the library asynchronously. The library is not loaded into memory at application startup; rather, it is loaded only when the developer explicitly calls the loadLibrary() method on the assigned prefix.
import 'package:heavy_computation/heavy_computation.dart' deferred as heavy;

Future<void> executeHeavyTask() async {
  // Explicitly load the library into memory
  await heavy.loadLibrary();
  
  // Access symbols via the prefix
  heavy.performCalculation();
}

Conditional Imports

Dart supports conditional imports to substitute different library implementations at compile time based on the target platform or environment. This is achieved using the if keyword within the import directive to evaluate configuration constants (such as dart.library.io or dart.library.html). The compiler resolves the first condition that evaluates to true and ignores the rest.
import 'package:example/default_impl.dart'
    if (dart.library.io) 'package:example/io_impl.dart'
    if (dart.library.html) 'package:example/web_impl.dart';
Master Dart with Deep Grasping Methodology!Learn More