flutter - =(等于)和:(冒号)默认参数初始化器之间的颤振差异
问题描述
我遇到了这段代码
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.0
,this.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);
现在我很困惑
解决方案
来自有效飞镖:
由于遗留原因,Dart 允许 : 和 = 作为命名参数的默认值分隔符。为了与可选的位置参数保持一致,请使用 =。
void insert(Object item, {int at = 0}) { ... } // good
void insert(Object item, {int at: 0}) { ... } // bad
推荐阅读
- html - justify-content-between 不适用于导航栏
- react-native - 将图像作为请求正文添加到 http 请求
- components - 在我使用 Component 之后,这种智能感知不起作用。问题是什么?
- rust - 有没有办法在 proc-macro crate 中具有公共特征?
- javascript - 使用 Jquery 添加按钮与调用函数时在 HTML 中显式添加的区别
- python - 使用正则表达式根据限制字符拆分字符串
- c++ - 是否可以从 c++ 类模拟私有/受保护的方法而不从它继承?
- json - 在 Django 中排除多对多关系的记录
- vue.js - 简化 Vuetify 工具提示的使用
- visual-studio-code - 当 SSH 扩展处于活动状态时激活另一个扩展