Kri*_*hna 3 timer operator-keyword dart 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();
Run Code Online (Sandbox Code Playgroud)
?.
[条件成员访问] - 类似.
,但最左边的操作数可以是null
; 示例:从表达式中foo?.bar
选择属性bar
,foo
除非foo
是null
(在这种情况下,值为foo?.bar
空)
TLDR:它只是null
在访问成员之前进行检查。如果运算符的左侧不为 null,则它的工作方式很简单.
,如果它是null
value,则整个事情都是null
.
在您的示例中:_debounceTimer?.isActive
- 如果_debounceTimer
为空则_debounceTimer?.isActive
<-> null
,如果_debounceTimer
不为空则_debounceTimer?.isActive
<-> _debounceTimer.isActive
。
还要检查:Dart Language tour (Conditional Expressions) for ??
and ?
operator。