[英]What is this unary postfix in dart/flutter?
我在Dart / flutter代码中看到了此一元后缀: ?.
像这样:
videoController?.dispose();
我想知道它是如何工作的...
这是Dart的一大特色
意思是当且仅当该对象不为null时,否则返回null 。
简单的例子:
void main() {
Person p1 = new Person("Joe");
print(p1?.getName); // Joe
Person p2;
print(p2?.getName); // null
//print(p2.getName); // this will give you an error because you cannot invoke a method or getter from a null
}
class Person {
Person(this.name);
String name;
String get getName => name;
}
还有其他一些很酷的null感知运算符,例如??
。 阅读我的QnA,以了解有关空感知运算符的更多信息。
它测试是否为空,
https://www.dartlang.org/guides/language/language-tour
“?。条件成员访问,类似于。,但是最左边的操作数可以为null;例如:foo?.bar从表达式foo中选择属性bar,除非foo为null(在这种情况下foo?.bar的值为null)”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.