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

A class in Dart is a blueprint for creating objects that encapsulates state through instance variables and behavior through methods. Dart is a purely object-oriented language, meaning every object is an instance of a class, and all classes ultimately descend from the `Object` class (with the exception of `Null`). Dart supports single inheritance, implicit interfaces, and mixin-based multiple inheritance.

## Anatomy of a Dart Class

A standard class definition includes instance variables (fields), constructors, and methods. Dart uses an underscore (`_`) prefix to denote library-private visibility for fields, methods, or the class itself.

```dart theme={"dark"}
class Entity {
  // Public instance variable
  String id;
  
  // Library-private instance variable
  int _internalState;

  // Generative constructor using initializing formal parameters
  Entity(this.id, this._internalState);

  // Instance method
  void updateState(int newState) {
    _internalState = newState;
  }
}
```

## Constructors

Dart provides multiple constructor types to handle different instantiation mechanics and memory management strategies.

* **Generative Constructors:** The standard mechanism for allocating a new instance.
* **Named Constructors:** Allows a class to define multiple constructors with distinct names.
* **Factory Constructors:** Uses the `factory` keyword. It does not strictly allocate a new instance; it can return an existing instance from a cache or an instance of a subtype.
* **Constant Constructors:** Uses the `const` keyword to create compile-time constants. All instance variables must be `final`.

```dart theme={"dark"}
class Vector {
  final double x;
  final double y;

  // Generative constructor
  Vector(this.x, this.y);

  // Named constructor with an initializer list
  Vector.origin() : x = 0.0, y = 0.0;

  // Constant constructor
  const Vector.immutable(this.x, this.y);

  // Factory constructor
  factory Vector.fromList(List<double> coordinates) {
    if (coordinates.length < 2) throw ArgumentError();
    return Vector(coordinates[0], coordinates[1]);
  }
}
```

## Properties (Getters and Setters)

Dart abstracts field access. Instance variables implicitly generate getters (and setters for non-final variables). You can override this behavior using the `get` and `set` keywords to compute properties dynamically.

```dart theme={"dark"}
class Box {
  double width;
  double height;

  Box(this.width, this.height);

  // Getter
  double get area => width * height;

  // Setter
  set area(double value) {
    width = value / height;
  }
}
```

## Inheritance, Interfaces, and Mixins

Dart handles class hierarchies and composition through three distinct keywords: `extends`, `implements`, and `with`.

* **`extends`:** Creates a subclass via single inheritance. The subclass inherits both the interface and the implementation of the superclass.
* **`implements`:** Treats a class as an interface. Every class in Dart implicitly defines an interface containing all its instance members. Using `implements` requires the subclass to provide implementations for all members of the target class, inheriting no behavior.
* **`with`:** Applies a mixin, allowing the reuse of a class's code in multiple class hierarchies without requiring a shared superclass.

```dart theme={"dark"}
class Base {}
class Contract {}
mixin Observable {}

class Concrete extends Base with Observable implements Contract {
  // Inherits implementation from Base and Observable
  // Must provide implementation for all members of Contract
}
```

## Class Modifiers (Dart 3+)

Dart 3 introduced class modifiers that strictly control how a class can be instantiated, extended, or implemented from outside its defining library.

* **`abstract`:** The class cannot be instantiated. It can contain abstract methods (methods without a body).
* **`base`:** The class can be instantiated and extended (`extends`), but cannot be implemented (`implements`). Guarantees the base class implementation is always executed.
* **`interface`:** The class can be instantiated and implemented, but cannot be extended.
* **`final`:** The class can be instantiated, but cannot be extended or implemented outside its library.
* **`sealed`:** The class cannot be instantiated. It creates a closed, exhaustively switchable type hierarchy. All direct subtypes must be defined in the same library.

```dart theme={"dark"}
// Cannot be instantiated; acts as a strict contract
abstract interface class Repository {
  void save();
}

// Exhaustive hierarchy root
sealed class Result {}
class Success extends Result {}
class Failure extends Result {}
```

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