Dart确保类中的可选List参数不为null

时间:2019-07-25 12:13:01

标签: class flutter dart null

padding必须是可选的,因此,如果我不传递任何填充参数,但与构造函数bold不同,它必须不接受任何值,而我必须将其设为0。 / p>

class QText extends StatelessWidget {
  QText(this.text, this.size,
      {this.align, this.bold = false, this.colour, this.font, this.padding});

  final String text;
  final double size;
  final bool bold;
  final Color colour;
  final String font;
  final TextAlign align;
  final List<double> padding;

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.fromLTRB(padding[0], padding[1],
          padding[2], padding[3]),
      child: Text(
          text,
          textAlign: align,
          style: TextStyle(
            fontSize: size,
            fontWeight: (bold) ? FontWeight.bold : FontWeight.normal,
            fontFamily: (font != '') ? font : '',
            color: colour,
          ),
        ),
    );
  }
}

我似乎无法设置QText(this.text, this.size, {this.align, this.bold = false, this.colour, this.font, this.padding = [0.0,0.0,0.0,0.0]]});或类似的东西,因为它会引发错误Default values of an optional parameter must be constant.,而且我也无法设置final List<double> padding ?? [0.0,0.0,0.0,0.0];

如果不传递参数,如何给它提供默认值?因为毕竟应该是可选的,但Padding小部件也不接受填充。

1 个答案:

答案 0 :(得分:2)

您尝试过:

QText(this.text, this.size, {this.align, this.bold = false, this.colour, this.font, this.padding = const <double>[0, 0, 0, 0]});