??
- It is a null-aware operator which returns the expression on its left unless that expression’s value is null, in which case it evaluates and returns the expression on its right:
print(1 ?? 3); // <-- Prints 1.
print(null ?? 12); // <-- Prints 12.
?.
- It is a conditional property access which is used to guard access to a property or method of an object that might be null, put a question mark (?) before the dot (.):
- You can chain multiple uses of
?.
together in a single expression:
myObject?.someProperty?.someMethod()
The preceding code returns null (and never calls someMethod()
) if either myObject
or myObject.someProperty
is null.