Sadakat Hussain Fahad
Sadakat Hussain Fahad

Reputation: 317

Syntax meaning for flutter

There is a syntax in the code "?."

The usage is ..

ClassName objectName;
objectName?.function();

Here the function is in the class. Why is the question mark used ?
what type of syntax is this?

Upvotes: 1

Views: 260

Answers (2)

barmacki
barmacki

Reputation: 41

This is a "conditional member access" operator (see official docs), which guarantees that the function() in question is not attempted on a null object.

Also, since dart 2.1.12 there is the neat "cascade notation", like so .., which allows you to "to make a sequence of operations on the same object". , You can combine it with the null-check operator ? to make ?..; this specifically is referred to as a "null-shorting cascade".

See official docs on that matter.

Example:


var submitButton = querySelector('#submit-button');
?..onClick.listen((_) => {}); //null-shorting necessary only on first call
 ..scrollIntoView();

Upvotes: 0

GrahamD
GrahamD

Reputation: 3165

It is a null aware operator. See this post: https://flutterigniter.com/checking-null-aware-operators-dart/

Specifically under the heading 'Safe Navigation Operator'

Upvotes: 2

Related Questions