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

A generic class in Dart is a class parameterized over types. It utilizes type variables (conventionally denoted by single uppercase letters such as `T`, `E`, `K`, or `V`) as placeholders for specific data types. This mechanism allows the class structure to enforce strict compile-time type safety across its fields, methods, and constructors without committing to a concrete type during declaration.

## Syntax and Declaration

To declare a generic class, append the type parameter enclosed in angle brackets (`< >`) immediately following the class name. The type parameter is then available in the class's lexical scope.

```dart theme={"dark"}
class Wrapper<T> {
  T data;

  Wrapper(this.data);

  T retrieveData() {
    return data;
  }
}
```

A class can declare multiple type parameters by separating them with commas.

```dart theme={"dark"}
class Pair<K, V> {
  final K key;
  final V value;

  Pair(this.key, this.value);
}
```

## Type Bounds (Constraints)

By default, a type parameter `T` is equivalent to `T extends Object?`, meaning it accepts any nullable or non-nullable type. To restrict the types that can be passed as a type argument, Dart uses the `extends` keyword to define a type bound.

If a type argument does not match the bound or a subtype of the bound, the Dart analyzer throws a compile-time error.

```dart theme={"dark"}
class NumericProcessor<T extends num> {
  final T value;

  NumericProcessor(this.value);

  double getDouble() => value.toDouble();
}

// Valid: int is a subtype of num
var intProcessor = NumericProcessor<int>(42);

// Invalid: String is not a subtype of num (Compile-time error)
// var stringProcessor = NumericProcessor<String>('42');
```

## Instantiation and Type Inference

When instantiating a generic class, the type argument can be explicitly declared. However, Dart's type inference engine can automatically resolve the type argument based on the constructor's arguments.

```dart theme={"dark"}
// Explicit type declaration
var explicitWrapper = Wrapper<String>('Dart');

// Inferred type declaration (Inferred as Wrapper<double>)
var inferredWrapper = Wrapper(3.14); 
```

## Reified Generics

Unlike languages that use type erasure (such as Java), Dart implements **reified generics**. This means that generic classes retain their type arguments at runtime. You can perform runtime type checks against the specific parameterized type.

```dart theme={"dark"}
var stringWrapper = Wrapper<String>('Hello');

// Evaluates to true at runtime
print(stringWrapper is Wrapper<String>); 

// Evaluates to false at runtime
print(stringWrapper is Wrapper<int>); 

// Accessing the runtime type directly
print(stringWrapper.runtimeType); // Outputs: Wrapper<String>
```

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