【问题标题】:What means the operator "??" in Dart/Flutter? [duplicate]运算符“??”是什么意思在飞镖/颤振? [复制]
【发布时间】:2021-02-16 05:11:17
【问题描述】:

我已经看过这段代码,需要解释一下“??”。 我知道像“?”这样的三元运算符然后是真条件,在“:”之后是假/其他条件。 但是双“??”是什么意思? ?

提前致谢

      widget.secondaryImageTop ??
      (widget.height / 2) - (widget.secondaryImageHeight / 2); ```

【问题讨论】:

标签: flutter dart operators ternary


【解决方案1】:

List of all dart operators

这是合并运算符。

a ?? b

表示:如果 a 不为 null,则解析为 a。如果a 为空,则解析为b

SQL 和其他一些语言都有这个运算符。

【讨论】:

    【解决方案2】:

    你的例子:

    widget.secondaryImageTop ??
          (widget.height / 2) - (widget.secondaryImageHeight / 2);
    

    这将使用 widget.secondaryImageTop 除非它为 null,在这种情况下它将使用 (widget.height / 2) - (widget.secondaryImageHeight / 2)。

    源代码和详细信息,包括 dartpad,您可以在其中使用预先填充的示例进行尝试: https://dart.dev/codelabs/dart-cheatsheet

    该文档中的一个示例,也使用了 = 符号。

    ??= 赋值运算符,它只为变量赋值 如果该变量当前为空:

    int a; // The initial value of a is null.
    a ??= 3;
    print(a); // <-- Prints 3.
    
    a ??= 5;
    print(a); // <-- Still prints 3.
    

    【讨论】:

      【解决方案3】:

      这意味着 => 如果存在,如果不为空...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-02-27
        • 2020-08-31
        • 1970-01-01
        • 2019-02-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多