如何使用可选参数定义调用super的构造函数?

时间:2017-06-19 05:19:06

标签: dart

我想延长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);

但我有super的可选参数和语法问题。所以,我想要做的是:

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

但语法错了。所以我想知道我应该如何处理可选参数,以及是否有一种简单的方法来获取参数的并在我将它们传递给super时传递它们。

1 个答案:

答案 0 :(得分:3)

没有简写将所有参数传递给超类。

通过在命名参数前面添加名称来传递命名参数,因此:

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);

这与使用非构造函数的命名参数的方式相同。