【问题标题】:Why does Android Studio add "const" to widgets?为什么 Android Studio 向小部件添加“const”?
【发布时间】:2021-07-13 03:57:15
【问题描述】:

我现在正在通过 Udemy 课程学习 Flutter。导师总是这样写代码:

padding: EdgeInsets.all(8.0),

但是在 Android Studio 中,当您添加“带填充的包装”时,它会添加 const(这不是不可更改的变量吗?)。同样在 Flutter 文档中,它一直使用 const

padding: const EdgeInsets.all(8.0),

【问题讨论】:

    标签: flutter android-studio dart flutter-layout


    【解决方案1】:

    如果你知道这些值不会随着屏幕生命周期而改变,最好使用 const 变量,它们对性能更好。但是你可以不用const,差别不是很大。看到这个帖子:What is the difference between the "const" and "final" keywords in Dart?

    【讨论】:

    • 我知道“const”和“final”关键字。但我想知道为什么在它起作用的时候让它变得更复杂?
    • @FlutterNewbie 仅仅因为某件事以一种更简单的方式起作用并不意味着你应该这样做。
    • 我认为就像是建议使用 const 更好,所以尽可能使用,猜测我们不应该仅在运行时计算的值无法使用 const 时使用。但是今天的智能手机硬件非常强大,所以无论如何都没有太大区别;)
    猜你喜欢
    • 2022-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多