The `covariant` Keyword: Loosening Type Rules

The `covariant` keyword tells Dart's analyzer to relax its strict rules for method overriding, letting a subclass method accept a more specific parameter type. It's often used in Flutter widgets.
The `covariant` keyword tells Dart's analyzer to relax its strict rules for method overriding. It lets a subclass method accept a more specific parameter type than its parent, which is normally a type safety violation. This is common in Flutter, where a parent widget's method might accept a generic `Widget`, but a subclass needs a specific `Text` widget. The footgun: misusing `covariant` trades a compile-time error for a potential runtime crash if an incorrect type is passed.
Read the original → dart.dev
- #dart
- #flutter
- #type-system
- #oop
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.