covariant modifier allows a subclass to override a mutable field (or a method parameter) with a type that is a subtype of the type defined in the superclass. This keyword explicitly disables static type safety checks regarding the input parameter of the overridden member, deferring type validation to runtime.
Syntax
The keyword is placed before the variable declaration in the subclass or before the parameter in a method signature.Type System Mechanics
In Dart, return types (getters) are naturally covariant, meaning a subclass can return a subtype of the parent’s return type without special modifiers. However, method parameters and implicit field setters are contravariant or invariant to satisfy the Liskov Substitution Principle (LSP). Standard inheritance rules dictate that if a superclass accepts a parameter of typeA, the subclass must also accept A. Narrowing the parameter to a subtype B (where B extends A) violates this contract because the subclass can no longer handle all inputs valid for the superclass.
The covariant keyword instructs the analyzer to bypass this rule for mutable fields and parameters. It permits the subclass to tighten the input contract, accepting only a narrower type than the superclass.
Runtime Behavior and Safety
When a field is markedcovariant, the compiler injects a check into the generated setter. If a value is assigned that matches the superclass’s signature but not the subclass’s narrowed signature, the runtime throws a TypeError.
Consider the following class hierarchy:
Valid Assignment
Direct assignment works when the assigned value matches the narrowed type.Runtime Failure
If the subclass is upcast to the superclass, the static analyzer allows assignment of the wider type (Animal or Cat) because the variable is typed as Shelter. However, the runtime check enforces the covariant constraint of the underlying MouseShelter instance.
Covariance in Methods
While frequently used with fields, the keyword technically applies to parameters. A mutable field declarationType field; implicitly generates a setter set field(Type value). The covariant keyword applies to the value parameter of that implicit setter.
It can also be applied explicitly to standard method parameters:
Master Dart with Deep Grasping Methodology!Learn More





