Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: default constructor is already defined

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?

like image 303
user350325 Avatar asked Dec 15 '13 18:12

user350325


People also ask

How do you make two constructors in flutter?

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.

Is already defined in class Java?

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.

How do you make an empty constructor in flutter?

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.


1 Answers

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]) {}
}
like image 57
Alexandre Ardhuin Avatar answered Oct 01 '22 23:10

Alexandre Ardhuin