TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
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. Applyingcovariant to the parameter in the subclass resolves this.
Superclass Declaration
Thecovariant 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
Becausecovariant 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.
Master Dart with Deep Grasping Methodology!Learn More





