簡體   English   中英

什么? 在 Dart 中做什么?

[英]What does ?. do in Dart?

"?." 是什么意思語法在 Dart 語言中做什么? 我這里有一個來自 Flutter 的 scaffold.dart 代碼的例子:

_drawerKey.currentState?.open();

它是一個空安全運算符。

用 ?。 當您想在對象上調用方法/getter 時,如果該對象不為 null(否則返回 null)。

_drawerKey.currentState?.open();

僅當它不為空時才調用open()

更多信息: https : //medium.com/@thinkdigitalsoftware/null-aware-operators-in-dart-53ffb8ae80bb

要保護對可能為 null 的對象的屬性或方法的訪問,請在點 (.) 前放置一個問號 (?):

myObject?.anyProperty

前面的代碼等效於以下內容:

(myObject != null) ? myObject.anyProperty: null

您可以鏈接 ? 的多種用途。 一起在一個表達式中:

myObject?.anyProperty?.anyMethod()

如果 myObject 或 myObject.anyProperty 為 null,則前面的代碼返回 null(並且從不調用 anyMethod())。

有關更多信息,請閱讀官方文檔

暫無
暫無

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

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