Q:

Dart - Null Operators

I need something like:
if (variable != null) {
  otherVariable = variable;
}

Is there a neater way to write this with NullOperators? All I came up with is:
(variable != null) ? otherVariable = variable : null;

A:

There is currently no special operator in Dart which allows an assignment to be dependent on the assigned value being non-null.
The closest operator is ??= where otherVariable ??= variable; does the opposite of what you want: It assigns variable to otherVariable if otherVariable is null, not if variable is non-null.
You could use ?? and go with otherVariable = variable ?? otherVariable;, which assigns the current value of otherVariable back to itself if variable is null. It still performs an assignment in that case, which might be detectable if otherVariable is a setter.
I'd just go with if (variable != null) otherVariable = variable;. It's currently the shortest code which does exactly what you want: Assign to otherVariable only if variable is not null.

