Reputation: 1455
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?
Upvotes: 22
Views: 12282
Reputation: 34210
In dart you cannot have multiple constructor as other language like java.
You can define default constructor as
class FirstClass {
FirstClass() {}
}
OR
class FirstClass {
String name;
int age;
FirstClass(name, age) {
this.name = name;
this.age = age;
}
}
You can not have default as well as parameter constructor in it.
Upvotes: 0
Reputation: 76213
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]) {}
}
Upvotes: 30