FLUTTER INTERVIEW QUESTIONS

What are ??, ??=, ?., …? in Dart?

Jelena Lecic

--

Looking weird, don’t they?
Those are null-aware operators in Dart and they tend to shorten your code a lot.

??

Called also null operator. This operator returns expression on its left, except if it is null, and if so, it returns right expression:

void main() {
print(0 ?? 1); // <- 0
print(1 ?? null); // <- 1
print(null ?? null); // <- null
print(null ?? null ?? 2); // <- 2
}

??=

Called also null-aware assignment. This operator assigns value to the variable on its left, only if that variable is currently null:

void main() {
int value;
print(value); // <- null
value ??= 5;
print(value); // <- 5, changed from null
value ??= 6;
print(value); // <- 5, no change
}

?.

Called also null-aware access(method invocation). This operator prevents you from crashing your app by trying to access a property or a method of an object that might…

--

--