Let's say I have several classes which extend an abstract class. Now I want to pass a default value to a function argument, where the type of the argument is the abstract class. Dart expects a const value, and I couldn't create a const constructor for an abstract class. How can I pass a default value of the abstract class?
Sample code is as following:
class Main {
late A objOfA;
Main({ A nObjOfA = const B() }); // <===== Error here
}
abstract class A {
abstract String name;
abstract int id;
}
class B extends A {
@override
String name = "B";
@override
int id = 1;
}
class C extends A {
@override
String name = "C";
@override
int id = 1;
}
Here, how can I pass a default value of nObjOfA
in the constructor of Main
?