Issue
I would like to specialize fields (override) when a class
is extended or implemented in Dart 2.
See the code:
abstract class Abase {
String id;
List<Bbase> bbases;
}
abstract class Bbase {
String id;
}
class A implements Abase {
String id;
String name;
List<B> bbases; // 'A.bbases=' ('(List<B>) → void') isn't a valid override of 'Abase.bbases=' ('(List<Bbase>) → void').
}
class B implements Bbase {
String id;
}
class Abase
has the generic field List<Bbase> bbase
and in the class A
I would like to specialize this field with List<B> bbase
.
But this is not possible. This error is presented on analyzer
or build_runner
:
‘A.bbases=’ (‘(List) → void’) isn’t a valid override of
‘Abase.bbases=’ (‘(List) → void’).
- Is there a reason for does not accept this specialization of fields?
- Is there another way to specializate fields on
extends
or
implements
?
Solution
You have to use generics:
abstract class Abase<T extends Bbase> {
String id;
List<T> bbases;
}
abstract class Bbase {
String id;
}
class A implements Abase<B> {
String id;
String name;
List<B> bbases;
}
class B implements Bbase {
String id;
}
Answered By – Alexandre Ardhuin
Answer Checked By – Terry (FlutterFixes Volunteer)