Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The class does not have a default constructor

Tags:

dart

Webstorm errors The class Task does not have a default constructor on GreatTask, I expect Task being a default constructor of `Task.

I am looking forward to create a contract of a Task, such that upon Task.run(), for each member of the list chain Task.chain[i] gets executed as it was a member of Task, otherwise I expect an uncaught error.

I am wondering what should I correct as first, the code or the configuration of Webstorm.

abstract class Task {
  List chain;

  Task(this.chain);

  void run() {
    this.chain.forEach((el) => this.el());
  }
}


class GreatTask extends Task {
  List chain;

 GreatTask(this.chain);

  String hi() {
    return 'hi';
  }
}
like image 620
mechanicious Avatar asked Sep 15 '15 20:09

mechanicious


1 Answers

The "default constructor" is (technically) the one that is added if you don't add any constructors yourself. It'll be YourClass(): super();.

The term "default constructor" is also commonly used about any no-name zero-argument generative constructor. That's what the error here is saying. Because GreatTask(this.chain); is the same as GreatTask(this.chain): super(); and the superclass Task does not have a no-name zero-argument generative constructor for super() to refer to, you have an error.

Günther's answer solves the problem by making the GreatTask constructor call the existing Task(List list) constructor instead of the non-existing Task() constructor.

like image 102
lrn Avatar answered Oct 19 '22 18:10

lrn