In Dart, I have the following code:
class FirstClass {
FirstClass(a) {
}
}
class SecondClass extends FirstClass {
}
This causes and error on SecondClass
because the FirstClass
does not have a default constructor.
However when I try to add one.
class FirstClass {
FirstClass(a) {
}
FirstClass() {
}
}
It errors because the default constructor is already defined in FirstClass
.
The only way I can seem to make this work and not error is if the superclass does not implement any constructors at all. What am I doing wrong?
You can only have one unnamed constructor, but you can have any number of additional named constructors in Flutter. By using named constructor you can create multiple constructors in the same class. Each constructor will have a unique name. So that you can identify each of them.
The main reason for this is that somewhere in same package you have already defined a class with name A. which causes type A is already defined error.
First, you have two constructors with the same name. Second, the first of those (not that the order matters) does not initialize the color member. You must fix both of those, mostly by just deleting the first one.
In dart you can not have the same method/constructor name used several times (even with different parameters).
In your case you can either use named constructor to define 2 constructors :
class FirstClass {
FirstClass() {}
FirstClass.withA(a) {}
}
or define a
as optional and keep only one constructor :
class FirstClass {
FirstClass([a]) {}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With