> ## 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 Sealed Class

A `sealed` class in Dart is an implicitly abstract class that restricts its class hierarchy to a single library, enabling the compiler to perform exhaustive pattern matching on its subtypes. By applying the `sealed` modifier, you prevent the class from being extended or implemented from any external library.

```dart theme={"dark"}
// file: result_types.dart

sealed class Result {}

class Success extends Result {
  final String data;
  Success(this.data);
}

class Failure extends Result {
  final Exception error;
  Failure(this.error);
}
```

## Core Mechanics and Rules

* **Library Encapsulation:** All direct subtypes (classes that `extend` or `implement` the sealed class) must be declared in the exact same library (typically the same file) as the sealed class itself.
* **Mixin Interactions:** The `sealed` modifier cannot be applied to a `mixin` declaration (i.e., `sealed mixin` is an invalid construct in Dart). Additionally, a `sealed` class cannot be used as a mixin, as Dart does not support the `sealed mixin class` declaration.
* **Implicitly Abstract:** A sealed class cannot be instantiated directly. You do not need to (and cannot) combine the `sealed` modifier with the `abstract` modifier.
* **Exhaustiveness Checking:** Because the compiler is aware of every possible subtype within the library, it enforces exhaustiveness in `switch` statements and `switch` expressions. If a new subtype is added to the sealed class, the compiler will flag an error at every `switch` site that does not handle the new subtype.

```dart theme={"dark"}
// The compiler guarantees all subtypes are handled.
// No 'default' or '_' wildcard branch is required.
String evaluateResult(Result result) {
  return switch (result) {
    Success(:final data) => 'Data: $data',
    Failure(:final error) => 'Error: $error',
  };
}
```

## Subclass Inheritance Behavior

While the `sealed` class itself is strictly bound to its defining library, its subclasses are not implicitly sealed. By default, a subclass of a sealed class behaves like a standard Dart class and can be extended or implemented by external libraries.

To maintain strict control over the entire hierarchy, you must apply additional class modifiers (`final`, `base`, or `sealed`) to the subclasses:

```dart theme={"dark"}
sealed class Node {}

// Can be extended outside the library
class StandardNode extends Node {} 

// Cannot be extended or implemented outside the library
final class LeafNode extends Node {} 

// Can be extended, but cannot be implemented outside the library
base class RootNode extends Node {} 
```

## Constructor Behavior

Sealed classes can declare constructors. Because the class is implicitly abstract, these constructors cannot be invoked to create an instance of the sealed class itself. Instead, they are used to initialize shared state via `super` calls from the allowed subclasses, or they are defined as `factory` constructors that return instances of the subclasses.

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