Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Setting a required constructor property when extending a class

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

class Subclass extends Super{

    final String name;
    final Function? callback;

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

}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading