What is "?." operator in flutter

6,604

?. [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: 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.

Share:
6,604
Krishna
Author by

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, 2022

Comments

  • Krishna
    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();