> ## 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 Covariant Parameter

The `covariant` keyword in Dart overrides the standard static type checking rules for method parameters during inheritance, allowing a subclass to tighten the type of an overridden method's parameter to a subtype of the superclass's parameter type.

By default, Dart enforces the Liskov Substitution Principle (LSP), which dictates that method parameters in subclasses must be contravariant or invariant. This means a subclass cannot restrict an overridden method to accept a narrower type than the superclass method. The `covariant` keyword explicitly instructs the Dart analyzer to bypass this compile-time restriction and defer the type check to runtime.

## Syntax and Mechanics

When a subclass overrides a method, attempting to narrow the parameter type results in a compile-time error. Applying `covariant` to the parameter in the subclass resolves this.

```dart theme={"dark"}
class Animal {}
class Cat extends Animal {}

class Caretaker {
  void feed(Animal animal) {}
}

class CatCaretaker extends Caretaker {
  // Without 'covariant', the analyzer throws:
  // "The parameter 'animal' of 'CatCaretaker.feed' has type 'Cat', 
  // which does not match the corresponding type, 'Animal', in the overridden method."
  
  @override
  void feed(covariant Cat animal) {
    // Parameter is now strictly typed as 'Cat' within this scope
  }
}
```

## Superclass Declaration

The `covariant` keyword can also be applied directly to the parameter in the superclass or interface. When declared in the superclass, the covariance contract is inherited, and subclasses can narrow the parameter type without needing to redeclare the keyword.

```dart theme={"dark"}
abstract class Caretaker {
  // Declaring covariance at the abstraction level
  void feed(covariant Animal animal);
}

class CatCaretaker extends Caretaker {
  @override
  void feed(Cat animal) {
    // Valid: 'covariant' is implicitly inherited from Caretaker
  }
}

class Dog extends Animal {}

class DogCaretaker extends Caretaker {
  @override
  void feed(Dog animal) {
    // Valid: 'covariant' is implicitly inherited from Caretaker
  }
}
```

## Runtime Implications

Because `covariant` suppresses compile-time static analysis for that specific parameter, type safety is enforced by the Dart Virtual Machine (VM) at runtime. If an instance of the subclass is accessed via a superclass reference and an invalid type is passed, the compiler will allow it, but the VM will throw a `TypeError`.

```dart theme={"dark"}
Caretaker caretaker = CatCaretaker();

// Static analysis passes because Caretaker.feed accepts Animal.
// Runtime execution succeeds because Cat matches the covariant CatCaretaker signature.
caretaker.feed(Cat()); 

// Static analysis passes because Caretaker.feed accepts Animal.
// Runtime execution FAILS and throws a TypeError because CatCaretaker expects a Cat.
caretaker.feed(Animal()); 
```

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