[英]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.