Whats ??= operator in Dart

◇◆丶佛笑我妖孽 提交于 2020-12-12 05:55:47

问题


This is the new assignment operator I see in Flutter source code:

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

what's the meaning of this assignment operator?

example in Flutter source code


回答1:


??= is a new null-aware operators. Specifically ??= is null-aware assignment operator.

?? 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.




回答2:


?? is a null check operator.

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

if person.name is null, then name is assigned a value of “John”.

??= simply means “If left-hand side is null, carry out assignment”. This will only assign a value if the variable is null.

splashFactory ??= InkSplash.splashFactory;



回答3:


The ?? double question mark operator means "if null" take the following expression.



来源:https://stackoverflow.com/questions/64642572/whats-operator-in-dart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!