【问题标题】:How to define variable and constant in flutter?如何在flutter中定义变量和常量?
【发布时间】:2019-09-17 02:35:38
【问题描述】:

正如我们在 swift 中定义的那样

let 保持一致

变量与var

flutter中如何定义?

【问题讨论】:

    标签: android android-studio flutter dart


    【解决方案1】:

    变量:

    var number = 42;
    

    常量:

    如果您从不打算更改变量,请使用 final 或 const,或者 而不是 var 或除了类型。可以设置最终变量 只有一次; const 变量是编译时常量。 (常量 变量是隐式最终的。)最终的顶级或类变量 在第一次使用时初始化。

    final name = 'Bob'; // Without a type annotation
    final String nickname = 'Bobby';
    

    将 const 用于您希望成为编译时常量的变量。如果 const 变量在类级别,将其标记为 static const。在哪里 您声明变量,将值设置为编译时常量 例如数字或字符串文字、常量变量或 对常数的算术运算:

    const bar = 1000000; // Unit of pressure (dynes/cm2)
    const double atm = 1.01325 * bar; // Standard atmosphere
    

    更多:https://dart.dev/guides/language/language-tour#variables

    【讨论】:

      猜你喜欢
      • 2022-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多