【发布时间】:2021-07-28 03:32:59
【问题描述】:
直到今天我还没有看到这个飞镖代码建议。我很高兴遵循最佳实践,但老实说,出现在没有构造函数的有状态小部件中是没有意义的。我认为它可能与 @immutable 注释有关,但似乎与 dart 文档并没有真正的帮助有关。
Dart 文档 https://dart-lang.github.io/linter/lints/prefer_const_constructors.html
VSCode 中的代码建议
Prefer const literals as parameters of constructors on @immutable classes.dart. || Prefer const with constant constructors
问题:这是我需要关心的事情还是我的插件已打开 VSCode 出了问题?
显示所有小部件的代码示例。
Column(
children: [
Container(
margin: EdgeInsets.only(left: 20, right: 20),
height: 50,
decoration: BoxDecoration(
borderRadius: BorderRadius.all(
Radius.circular(30),
),
color: Colors.black,
),
child: Center(
child: Text(
'Create Account',
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.w600,
fontSize: 19),
),
),
),
SizedBox(
height: 20,
)
完整性筛选
【问题讨论】:
-
lint 告诉我们
TextStyle、SizedBox、Center等小部件的那些特定实例都是用编译时常量构造的,因此您应该将它们全部标记如const以避免在重新构建小部件树时在运行时重新构建它们。它完全适用于您的代码。