Use the Super Argument in Data Class in Dart

Anyone who’s written a few classes in Dart knows that to call a superclass constructor, you use the keyword ‘super’ followed by the appropriate arguments in parentheses. For example, to create a subclass of ‘Shape’ that takes a ‘String’ describing the shape, you might write something like this:

//before Dart 2.17
class Square extends Shape {
    final double radius;
    Square(String description, this.double) : super(description);
}

However, Dart 2.17 introduces a new shorthand syntax that makes it easier to pass constructor arguments to the superclass. Now, instead of using the keyword ‘super’, you can simply use super in the argument list. For example, the above code could be rewritten like this:

class Square extends Shape {
    final double radius;
    Square(super.description, this.double);
}

This makes it easy to add or remove several fields of the parent class without having to change the lots of codes.

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close