What is "?." operator in flutter
?.
[Conditional member access] - Like.
, but the leftmost operand can benull
; example:foo?.bar
selects propertybar
from expressionfoo
unlessfoo
isnull
(in which case the value offoo?.bar
is null)
TLDR: It is simply does a null
check before accessing member. If left hand side of the operator is not null then it works simply as .
and if it is null
value then the whole thing is null
.
In your example: _debounceTimer?.isActive
- if _debounceTimer
is null then _debounceTimer?.isActive
<-> null
and if _debounceTimer
is not null then _debounceTimer?.isActive
<-> _debounceTimer.isActive
.
Also check: Dart Language tour (Conditional Expressions) for ??
and ?
operator.
Krishna
Hi, I am Krishna Parshad and i am a full stack developer with 2 years of hands on experience on technologies like Flutter , Firebase , AWS , React , node j.s, Javascript , Python, etc. I am passionate developer with work ethics. Upwork https://www.upwork.com/o/profiles/users/~01136de6e71a23213e/?s=996364627857502209 Linkdin: www.linkedin.com/in/krishna-adhikari-b7a506161
Updated on December 18, 2022Comments
-
Krishna over 1 year
I am using a library and in many places ?. operator is used i am unable to understand it's purpose.
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();