Syntax Specification
The syntax appendsif clauses to a standard import or export directive. The compiler evaluates conditions sequentially from top to bottom. The first condition that evaluates to true determines the file to be included. If no conditions are met, the default URI is used.
Condition Evaluation
The condition inside theif clause evaluates environment declarations. These declarations are key-value pairs defined in the compilation environment.
- Platform Availability: The Dart compiler automatically sets boolean flags indicating which standard libraries are available.
dart.library.io: True for native platforms (VM, Desktop, Mobile).dart.library.js_interop: True for modern Web (JS and Wasm).dart.library.html: True for legacy Web (JS only).
- Arbitrary Constants: Conditions can evaluate user-defined constants passed via the
-Dcommand-line flag (e.g.,dart compile exe -Dmy.flag=true main.dart).
- Boolean Check:
if (key)evaluates totrueif the key exists and its value is"true". - Equality Check:
if (key == 'value')evaluates totrueif the key exists and matches the string value.
Architectural Requirements
To ensure type safety and compilation success, the default library (the stub) and all conditional targets must adhere to a strict interface contract:- Symbol Parity: Every class, function, or variable exposed by the stub must exist in every platform-specific implementation.
- Signature Matching: The return types and parameter lists of functions must match exactly across all implementations.
- Abstraction: Consuming code must rely solely on the interface defined in the stub, remaining unaware of the underlying platform-specific implementation.
Implementation Pattern
The standard implementation structure involves a stub file, platform-specific files, and a main interface file.1. The Stub (Default)
This file defines the public API and throwsUnimplementedError. It serves as the fallback for platforms not covered by the conditions and provides the interface for the IDE/analyzer.
2. Platform Implementations
These files import platform-specific libraries and implement the API defined in the stub. Native Implementation:3. The Conditional Directive
The main entry point uses a conditional export directive to route the request to the correct file based on the compiler flags. This allows consumers to import a single file (manager.dart) while receiving the correct implementation for their target platform.
Master Dart with Deep Grasping Methodology!Learn More





