covariant keyword is a modifier that disables static enforcement of parameter contravariance during method overriding. It permits a subclass to override a method parameter with a type that is a subtype of the parameter in the superclass, effectively tightening the input requirements.
Type System Mechanics
In Dart’s sound type system, method parameters are normally contravariant. To satisfy the Liskov Substitution Principle, an overriding method must accept the same parameter type or a supertype (wider type) as the method it overrides. Thecovariant keyword explicitly inverts this rule for a specific parameter, allowing covariance (accepting a narrower type). This transfers type safety checks for that parameter from compile-time to runtime.
Syntax and Behavior
The keyword is placed before the parameter type in the method signature of the subclass.Application on Fields
When applied to an instance field,covariant applies to the implicit setter of that field. This allows a subclass to override a mutable field with a more specific type.
The modifier cannot be applied to final fields because they lack setters. Additionally, overriding a getter (the read operation) with a more specific return type is inherently allowed by the type system and does not require the covariant modifier.
Runtime Implications
Usingcovariant bypasses static analysis, introducing the possibility of runtime exceptions. The Dart runtime injects a check to ensure the object passed matches the narrowed type.
If a client treats the subclass instance as the superclass type (upcasting) and passes an argument that is valid for the superclass but invalid for the subclass, a TypeError is thrown.
Implicit Covariance in Generics
Dart generics are covariant by default. Consequently, parameters defined using a class’s generic type variable are implicitly covariant. This means that aBox<int> is a subtype of Box<Object>, even though Box<int> exposes a method accepting int while Box<Object> exposes a method accepting Object.
Master Dart with Deep Grasping Methodology!Learn More





