Skip to main content
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.

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.

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.
Tired of Poor Dart Skills? Fix That With Deep Grasping!Learn More