Reputation: 99
Update: As a clarification by "get rid" of the annotation I mean suppressing the warnings the analyzer throws without changing anything in the analyzer itself
I'm extending a class with an annotation and I want to override the parent class annotation, more particularly, get it rid of it. Is it possible to do so?
For example:
@immutable
class A {}
/// get rid of @immutable annotation
class MutableA extends A {}
Upvotes: 2
Views: 1248
Reputation: 71743
Annotations have no semantics in the Dart language itself, they mean what the tools recognizing them decide that they mean.
Annotations are never inherited. If you check using dart:mirrors
, annotations only exist where they are written in the source code. Annotations are on declarations, not values.
So, if a tool recognizing @immutable
, and it decides that the property of being "immutable" is "inherited" by subclasses, then that's that tool's decision.
The immutable
annotation is documented as applying to "all subtypes".
From the language perspective, there is no @immutable
on MutableA
, it's all in the head of the tool, so there is not annotation to get rid of. The meaning of the annotation on A
is that all subclasses of A
must be immutable.
The message that you get from the analyzer is a hint. You can suppress hints in various way, for example by adding an "ignore" comment before the line causing the hint. In this case, you can add
// ignore: must_be_immutable
on the line before the class declaration of the subclass which is not immutable. That should suppress the hint.
Upvotes: 5