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

A covariant field in Dart is an instance variable modified by the `covariant` keyword, allowing a subclass to override the field with a stricter (narrower) subtype than the one declared in its superclass. This explicitly bypasses Dart's default static type-checking rules, which normally require setter parameters to be contravariant or invariant.

In Dart, declaring a mutable field implicitly generates both a getter and a setter. According to standard object-oriented subtyping rules, overriding a getter allows for a covariant return type, but overriding a setter requires a contravariant parameter type. Because a field represents both a getter and a setter, overriding a field with a narrower type creates a static type error on the implicit setter.

The `covariant` keyword suppresses this compile-time error on the implicit setter and defers the type validation to runtime.

```dart theme={"dark"}
class SuperType {}
class SubType extends SuperType {}

class BaseClass {
  SuperType? myField;
}

class DerivedClass extends BaseClass {
  // Narrows the type of myField from SuperType? to SubType?.
  // The 'covariant' keyword prevents a static analysis error.
  @override
  covariant SubType? myField; 
}
```

Alternatively, the `covariant` keyword can be applied directly to the field in the base class. This establishes a contract that permits any future subclass to tighten the field's type without needing to redeclare the keyword:

```dart theme={"dark"}
class BaseClass {
  // Declares that subclasses may narrow this field's type
  covariant SuperType? myField;
}

class DerivedClass extends BaseClass {
  @override
  SubType? myField; // Valid, covariance was established in the superclass
}
```

**Mechanics and Type Safety**

* **Static Analysis:** When `covariant` is applied, the Dart analyzer permits the narrower type declaration. Within the subclass, the field is statically recognized as the narrower type, allowing direct access to the subtype's specific members without manual casting.
* **Runtime Enforcement:** By using `covariant`, type safety for the implicit setter is shifted from compile-time to runtime. If an instance of the subclass is upcast to the base class, and an incompatible sibling type is assigned to the field, the Dart Virtual Machine will throw a `TypeError` during execution.

```dart theme={"dark"}
BaseClass instance = DerivedClass();

// Compiles successfully due to the BaseClass reference, 
// but throws a runtime TypeError because 'instance' is actually 
// a DerivedClass, which expects a SubType?, not a base SuperType.
instance.myField = SuperType(); 
```

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