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();
?.[Conditional member access] - Like., but the leftmost operand can benull; example:foo?.barselects propertybarfrom expressionfoounlessfooisnull(in which case the value offoo?.baris 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With