Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define a constructor calling a super with optional arguments?

Tags:

dart

I would like to extend Text class which has following constructor:

  const Text(this.data, {
    Key key,
    this.style,
    this.textAlign,
    this.softWrap,
    this.overflow,
    this.textScaleFactor,
    this.maxLines,
  }) : assert(data != null),
       super(key: key);

But I have issues with super's optional parameters and syntax. So, what I am trying to do is something like:

BlinkingText(data, 
    {key, 
     style, 
     textAlign, 
     softWrap, 
     overflow, 
     textScaleFactor, 
     maxLines}):
             super(data, {key, style, textAlign, softWrap, overflow, textScaleFactor, maxLines});

But syntax is wrong. So I wonder how should I deal with optional arguments and if there is a simple way to get a bunch of arguments and pass them as I get them to super.

like image 980
RobertoAllende Avatar asked Jun 19 '17 05:06

RobertoAllende


1 Answers

There is no shorthand for passing all your arguments to the super-class.

You pass named arguments by prefixing them with the name, so:

BlinkingText(data, 
    {key, 
     style, 
     textAlign, 
     softWrap, 
     overflow, 
     textScaleFactor, 
     maxLines})
    : super(data, key: key, style: style, textAlign: textAlign, 
        softWrap: softWrap, overflow:overflow, 
        textScaleFactor: textScaleFactor, maxLines: maxLines);

That's the same way you use named arguments with non-constructor functions.

like image 189
lrn Avatar answered Oct 21 '22 04:10

lrn