【问题标题】:Whats ??= operator in DartDart 中的 ??= 运算符是什么
【发布时间】:2020-11-02 09:04:15
【问题描述】:

这是我在 Flutter 源码中看到的新赋值运算符:

splashFactory ??= InkSplash.splashFactory;
textSelectionColor ??= isDark ? accentColor : primarySwatch[200];

这个赋值运算符是什么意思?

example in Flutter source code

【问题讨论】:

标签: flutter dart operators


【解决方案1】:

??= 是一个新的 null 感知运算符。具体来说 ??= 是可识别空值的赋值运算符。

?? 如果为空运算符。 expr1 ?? expr2 如果不是 null,则计算为 expr1,否则为 expr2

??= 空感知分配。 v ??= expr 导致分配 v expr 仅当 vnull

?。 零感知访问。如果x 不是null,则x?.p 计算为x.p,否则计算为null

【讨论】:

    【解决方案2】:

    ?? 是一个空检查运算符。

    String name=person.name ?? 'John';
    

    如果 person.name 为 null,则 name 被赋值为“John”。

    ??= 仅表示“如果左侧为空,则执行赋值”。这只会在变量为空时赋值。

    splashFactory ??= InkSplash.splashFactory;
    

    【讨论】:

      【解决方案3】:

      ??双问号运算符表示“如果为空”,取如下表达式。

      【讨论】:

        猜你喜欢
        • 2021-04-02
        • 2012-05-11
        • 2013-07-31
        • 1970-01-01
        • 1970-01-01
        • 2010-12-19
        • 2021-12-04
        • 2021-10-28
        • 2021-02-13
        相关资源
        最近更新 更多