As of Dart 1.12 null-aware operators are available for this type of situation:
bool isConnected(a, b) {
bool outConn = outgoing[a]?.contains(b) ?? false;
bool inConn = incoming[a]?.contains(b) ?? false;
return outConn || inConn;
}
The ?.
operator short-circuits to null if the left-hand side is null, and the ??
operator returns the left-hand side if it is not null, and the right-hand side otherwise.
The statement
outgoing[a]?.contains(b)
will thus either evaluate to null
if outgoing[a]
is null
, or the boolean result of contains(b)
if it is not.
That means the resulting statement will be one of the following:
bool outConn = null ?? false; // false
bool outConn = false ?? false; // false
bool outConn = true ?? false; // true
The same applies to the inConn
boolean, which means both inConn
and outConn
are guaranteed to be non-null, allowing us to return the result of ||
ing the two.