I was trying to implement loose coupling in one of my Flutter projects. It was not able to find the method. Have replicated the same in a simple Dart code, how can I fix this, and is there some way to achieve loose coupling in Dart?
abstract class A{}
class B extends A{
void help(){
print("help");
}
}
class C {
A b;
C({required this.b});
void test(){
b.help();
}
}
void main() {
var c = C(b:B());
c.test();
}
Giving error at b.help(), the method does on exist.
Exact error
The method 'help' isn't defined for the type 'A'.
bis known to be of typeA, and theAinterface does not provide ahelpmethod.I don't know exactly what your definition of "loose coupling" is (it'd be better to describe a specific problem that you're trying to solve), but if you want
helpto be callable on typeA, then you must add it to theAinterface.You alternatively could explicitly downcast
btoBwith a runtime check:Or if you want duck typing, you could declare (or cast)
basdynamic:although I would consider the last form to be bad style.