簡體   English   中英

飛鏢/顫振中的一元后綴是什么?

[英]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)”

它是一個空值 運算符 這是以下內容的簡稱。

 ((obj) => obj == null ? null : x.method())(object)
 // is equal to
 object?.method()

您可以在此處找到有關空感知運算符的更多信息

說明

讀為:

  • 僅當object不為null時才執行method

  • 如果objectnull返回null (否則從method求值)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM