Flutter空操作符

?? & ??=

var expr1;
var expr2 = 'b';
expr1 = expr1 ?? expr2;

如果expr1为null,那么将expr2赋值给expr1, 反之如果不为null,就将expr1赋值给expr1.等同于:

expr1 ??= expr2;

等同于:

if(expr1 == null){
  expr1  = expr2
}

?.

class Person{
   String name = "Tony"
}

var userName ;
late Person p = Person();
userName = p?.name;
print(userName)

userName = p?.name; 等同于

if(p == null){
  userName = null;
}else {
  userName = p.name;
}

你可能感兴趣的:(Flutter空操作符)