什么是 ”?。”颤振中的运算符

What is "?." operator in flutter

我在很多地方使用图书馆?。 使用运算符我无法理解它的用途。

Timer _debounceTimer;
  @override
  initState() {
    _textController.addListener(() {
      // We debounce the listener as sometimes the caret position is updated after the listener
      // this assures us we get an accurate caret position.
      if (_debounceTimer?.isActive ?? false) _debounceTimer.cancel();

?. [Conditional member access] - Like ., but the leftmost operand can be null; example: foo?.bar selects property bar from expression foo unless foo is null (in which case the value of foo?.bar is null)

from Dart Language Tour (Other Operators)

TLDR:它只是在访问成员之前进行 null 检查。如果运算符的左侧不为空,那么它的工作原理就像 .,如果它是 null 值,那么整个事情就是 null.

在您的示例中:_debounceTimer?.isActive - 如果 _debounceTimer 为空,则 _debounceTimer?.isActive <-> null,如果 _debounceTimer 不为空,则 _debounceTimer?.isActive <-> _debounceTimer.isActive.

同时检查:Dart Language tour (Conditional Expressions) ??? 运算符。