Home > other >  Setting a required constructor property when extending a class
Setting a required constructor property when extending a class

Time:10-07

If I have a class like this:

Class Super {
    final String name;
    final Function() callback;

    const Super({required this.name, required this.callback});
}

And I want to extend that class, and my sub-class will take the required "name" in its constructor also, but I want my sub-class to implement the callback function so that user of my sub-class does not have to provide this function (actually I want to prevent them for doing so if possible, but at least they do not have to as it is implemented bt the sub-class). Is this possible, and how?

Best regards

CodePudding user response:

class Subclass extends Super{

    final String name;
    final Function? callback;

    const Subclass({required this.name,this.callback}) :
    super(name : name,callback: callback ?? (){});

}
  •  Tags:  
  • dart
  • Related