【问题标题】:Flutter const with const constructorsFlutter const 与 const 构造函数
【发布时间】: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 告诉我们TextStyleSizedBoxCenter 等小部件的那些特定实例都是用编译时常量构造的,因此您应该将它们全部标记如const 以避免在重新构建小部件树时在运行时重新构建它们。它完全适用于您的代码。

标签: flutter dart


【解决方案1】:

Lint 解释器总是会向您展示即兴编码和性能优化技巧的最佳方法。

例如:在您的代码中,有许多小部件不会重新构建。因为它是编译时常量,因为它们不依赖于其他动态属性。

Column(
                children: [
                  Container(
                    margin: const EdgeInsets.only(left: 20, right: 20),
                        height: 50,
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.all(
                            Radius.circular(30),
                          ),
                          color: Colors.black,
                        ),
                        child: const Center( // compile time constant widget
                          child: const Text( // compile time constant widget
                            'Create Account',
                            style: TextStyle(
                                color: Colors.white,
                                fontWeight: FontWeight.w600,
                                fontSize: 19),
                          ),
                        ),
                      ),
                      const SizedBox( // compile time constant widget
                        height: 20,
                      )

假设您的 Text 小部件值取决于其他变量 x,而不是静态值 “创建帐户” 那么您不能在 Text Widget 之前添加 const 关键字,因为它不是编译时常量价值。

即使您不担心这种 lint 建议,也没关系。但是,如果您考虑 lint 建议,那么您在开发中的漫长旅程将会非常顺利

【讨论】:

  • 感谢您的回复。虽然这会在没有不必要的小部件重建的情况下提高性能,但如果组件是可变的,那么我如何标记它以避免我的所有代码显示这些分散注意力的行?
  • 一旦您的组件实际更改了这些,它将停止被标记。如果用变量替换编译时常量,linter 将停止。但是如果你的组件实际上并没有改变这些值,它可以是 const 并且应该是 const。请注意,linter 并不是要让您的组件不可变。只是组件中实际上没有改变的部分。
  • 谢谢,这是有道理的,虽然我觉得 Dart 应该足够聪明,可以确定变量是否与小部件相关联,但事实上我认为这就是它的工作原理。如果可以推断出,使 const 显式化似乎是一个乏味的过程。不管怎样,谢谢你的回答。
【解决方案2】:

为避免带有常量构造函数的 prefer const 警告,请将此规则 prefer_const_constructors : false 添加到 analysis_options.yaml 文件中。

linter:
rules:
prefer_const_constructors : false
# avoid_print: false  # Uncomment to disable the `avoid_print` rule
# prefer_single_quotes: true  # Uncomment to enable the 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-17
    • 2021-11-10
    • 1970-01-01
    • 1970-01-01
    • 2018-08-15
    • 1970-01-01
    • 2014-11-03
    • 1970-01-01
    相关资源
    最近更新 更多