首页 > 解决方案 > =(等于)和:(冒号)默认参数初始化器之间的颤振差异

问题描述

我遇到了这段代码

 const CustomChip({
    Key key,
    this.leading,
    this.trailing,
    this.title,
    this.backgroundColor,
    this.height: 30.0,
    this.elevation = 2.0,
    this.onTap,
  }) : super(key: key);

两者之间有什么区别this.height: 30.0this.height = 30.0似乎两者都未定义会使高度为 30.0

编辑

我也发现了这样的东西

const CustomChip({
    Key key,
    this.leading,
    this.trailing,
    this.title,
    this.backgroundColor,
    double height,
    this.elevation = 2.0,
    this.onTap,
  })
      : height = height ?? 30.0,
        super(key: key);

现在我很困惑

标签: flutterdart

解决方案


来自有效飞镖

由于遗留原因,Dart 允许 : 和 = 作为命名参数的默认值分隔符。为了与可选的位置参数保持一致,请使用 =。

void insert(Object item, {int at = 0}) { ... } // good
void insert(Object item, {int at: 0}) { ... } // bad

推荐阅读