I have an issue to override a generic method from an abstract class.
Here is my abstract class:
abstract class A {
String getData<Type>(Type key);
}
when I created a class (B) to implement class (A) as showing below:
class B implements A {
@override
String getData<String>(String key) { //Error <= B.getData' ('String Function<String>(String)') isn't a valid override of 'A.getData' ('String Function<Type>(Type)')
return "";
}
}
Shown the below compilation error at (getData) method:
'B.getData' ('String Function(String)') isn't a valid override of 'A.getData' ('String Function(Type)'). dart(invalid_override)
And this error at the return statement:
A value of type 'String can't be returned from method 'getData' because it has a return type of 'String'.
It's confusing for me why I am getting this error related to the override when the generic type similar to return type. as when I created another class (C) with getData<int>(int key)
instead of getData<String>(String key)
everything works as expected:
class C implements A {
@override
String getData<int>(int key) {
return "";
}
}
Also with the class (B) if I deleted the return type as shown below everything will work as expected:
class B implements A {
@override
getData<String>(String key) {
return "";
}
}
Is it an issue in Dart design so I can be aware of it because this behavior does not exist in c# language for example?
DartPad Link: https://dartpad.dev/ebc7e6361d0cf1c6afad2f52ab8ebcaa