什么是 ??= Dart 中的运算符
Whats ??= operator in Dart
这是我在Flutter源码中看到的新赋值运算符:
splashFactory ??= InkSplash.splashFactory;
textSelectionColor ??= isDark ? accentColor : primarySwatch[200];
这个赋值运算符是什么意思?
那个??双问号运算符表示“如果为空”,请采用以下表达式。
?? 是空检查运算符。
String name=person.name ?? 'John';
如果 person.name 为 null,则 name 被赋予值“John”。
??= 简单的意思是“如果 left-hand 边为空,执行赋值”。这只会在变量为空时分配一个值。
splashFactory ??= InkSplash.splashFactory;
??= 是一个新的 null-aware 运算符。具体 ??= 是 null-aware 赋值运算符。
??
if null operator. expr1 ?? expr2
evaluates to expr1
if not null
, otherwise expr2
.
??=
null-aware assignment. v ??= expr
causes v
to be assigned
expr
only if v
is null
.
?.
null-aware access. x?.p
evaluates to x.p
if x
is not null
, otherwise evaluates to null
.
这是我在Flutter源码中看到的新赋值运算符:
splashFactory ??= InkSplash.splashFactory;
textSelectionColor ??= isDark ? accentColor : primarySwatch[200];
这个赋值运算符是什么意思?
那个??双问号运算符表示“如果为空”,请采用以下表达式。
?? 是空检查运算符。
String name=person.name ?? 'John';
如果 person.name 为 null,则 name 被赋予值“John”。
??= 简单的意思是“如果 left-hand 边为空,执行赋值”。这只会在变量为空时分配一个值。
splashFactory ??= InkSplash.splashFactory;
??= 是一个新的 null-aware 运算符。具体 ??= 是 null-aware 赋值运算符。
?? if null operator.
expr1 ?? expr2
evaluates toexpr1
if notnull
, otherwiseexpr2
.
??= null-aware assignment.
v ??= expr
causesv
to be assignedexpr
only ifv
isnull
.
?. null-aware access.
x?.p
evaluates tox.p
ifx
is notnull
, otherwise evaluates tonull
.