> ## 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.

# Dart Deferred Import

Deferred imports allow a Dart application to load a library on demand at runtime rather than during the initial application startup. This mechanism instructs the compiler to isolate the deferred code into a separate loadable unit (where supported by the compilation target), which is only fetched, parsed, and executed when explicitly requested via an asynchronous call.

To define a deferred import, use the `deferred as` keywords followed by a mandatory namespace identifier.

```dart theme={"dark"}
import 'package:my_package/heavy_library.dart' deferred as heavy;
```

Before accessing any members (functions, classes, or variables) of the deferred library, you must explicitly invoke the `loadLibrary()` method on the defined namespace. This method returns a `Future<void>` that completes once the library is fully loaded into the isolate's memory.

```dart theme={"dark"}
Future<void> executeDeferredCode() async {
  // Asynchronously load the library
  await heavy.loadLibrary();
  
  // Access members using the namespace prefix
  var instance = heavy.HeavyClass();
  instance.performAction();
}
```

## Technical Mechanics and Constraints

When implementing deferred imports, the Dart compiler and runtime enforce strict rules regarding how the importing file interacts with the deferred library:

* **Type Annotation Restrictions:** You cannot use types from a deferred library as static type annotations (such as in variable declarations, return types, or method signatures) in the importing file. Doing so results in a strict compile-time error. While the underlying reason for this rule relates to runtime memory mechanics—requiring all explicitly declared types to be available in the isolate's memory when the importing library loads—the restriction is enforced by the analyzer and compiler at compile time. To reference an instance of a deferred type, the idiomatic approach is to rely on type inference using `var` or `final`. The analyzer will infer the deferred type and provide static type checking without requiring an explicit annotation. Alternatively, you can type the variable as `dynamic` or use a shared base class/interface that is imported synchronously.
* **Compile-Time Constants:** Constants defined in a deferred library are not treated as compile-time constants in the importing file. They cannot be used in `const` contexts (e.g., as default parameter values or inside `const` collections) because their values are not initialized until the library is loaded at runtime.
* **Idempotency:** The `loadLibrary()` function is idempotent. You can invoke `await heavy.loadLibrary()` multiple times across your application; the Dart runtime guarantees that the library is only fetched and initialized once. Subsequent calls will return a completed `Future` immediately.
* **Compilation Output:**
  * **Dart Web (dart2js):** The compiler physically splits the deferred library and its dependencies into separate JavaScript files (fragments). These fragments are downloaded over the network only when `loadLibrary()` is invoked.
  * **Dart Native (AOT/JIT):** Standard Dart Native (the Dart VM and `dart compile exe`) does not support splitting deferred code into separate files. It compiles all deferred code into a single executable and loads it upfront. The `loadLibrary()` call is still required for API compatibility and returns a completed `Future`, but the code is not physically deferred into separate dynamic libraries. (Note: Splitting AOT code into separate downloadable libraries is a Flutter-specific feature for certain deployment targets, not a standard Dart Native capability).

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Dart Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
