Skip to main content
Dart core library imports utilize the dart: URI scheme to reference built-in libraries provided directly by the Dart SDK. These libraries are distinct from package imports (package:) and relative file imports, as they are resolved by the Dart compiler or runtime environment without requiring external dependencies in pubspec.yaml.

Syntax and Placement

Import directives must appear at the top of the Dart file. They must follow any library directives but strictly precede part directives and all top-level code declarations.
// General syntax
import 'dart:library_name';

// Example: Importing the asynchronous programming library
import 'dart:async';

Implicit Imports

The dart:core library is implicitly imported into every Dart program. This library exposes fundamental types (e.g., int, String, List, Map) and basic utilities (e.g., print). Explicitly importing dart:core is redundant.

Namespace Management

Core library imports support standard namespace modifiers to handle naming conflicts or limit scope. Prefixing Assigns an alias to the library to avoid identifier collisions.
import 'dart:math' as math;

void main() {
  // Access members via the prefix
  print(math.pi); 
}
Filtering Restricts the symbols imported into the current namespace using show (inclusion) or hide (exclusion).
// Only import the Future class
import 'dart:async' show Future;

// Import everything except the jsonDecode function
import 'dart:convert' hide jsonDecode;

Platform Specificity

While many core libraries are multi-platform, specific libraries are restricted to certain compilation targets or have varying support depending on the backend (Native, JavaScript, or Wasm).
  • Universal: Libraries such as dart:async, dart:collection, dart:convert, and dart:math are available on all supported Dart targets.
  • Native (VM/AOT): The dart:io library provides I/O APIs (files, sockets, HTTP servers) and is strictly available only when running on the Dart VM or compiled to native machine code. It is not supported on the web.
  • Web:
    • Legacy: Libraries such as dart:html and dart:indexed_db are available only when compiling to JavaScript.
    • Modern Interop: dart:js_interop is available for both JavaScript and WebAssembly (Wasm) targets.
  • Cross-Platform with Constraints:
    • dart:isolate: Supported on Native and Web (both JS and Wasm), though implementation details (such as concurrency models) differ between the VM and the Web.
    • dart:ffi: Supported on Native and Web (Wasm only). It is not supported when compiling to JavaScript.

Conditional Imports

The import directive supports configuration-specific logic to import different libraries based on the compilation target. This is achieved using the if keyword followed by a library condition (e.g., dart.library.io or dart.library.js_interop). Requirement for Shared Interfaces When using conditional imports, all potential import targets must expose the exact same API (classes, methods, and properties) used by the consuming code. If the APIs differ, the code will fail static analysis.
// Syntax: import 'default_library' if (condition) 'specific_library';

// Example:
// Imports 'stub_library.dart' by default.
// If the compiler detects 'dart:io' support, it imports 'native_implementation.dart' instead.
import 'stub_library.dart' if (dart.library.io) 'native_implementation.dart';
Master Dart with Deep Grasping Methodology!Learn More