Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is "?." operator in flutter

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();
like image 724
Krishna Avatar asked Sep 19 '25 15:09

Krishna


1 Answers

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

like image 116
Harsh Bhikadia Avatar answered Sep 21 '25 07:09

Harsh Bhikadia