Miscellaneous

Conditional Property Access

userObject?.userName
//The code snippet above is equivalent to following:
(userObject != null) ? userObject.userName : null
//You can chain multiple uses of ?. together in a single expression
userObject?.userName?.toString()
// The preceeding code returns null and never calls toString() if either userObject or userObject.userName is null

Cascade notation (..)

// allows you to make a sequence of operations on the same object
// rather than doing this
var user = User();
user.name = "Nicola";
user.email = "[[email protected]](/cdn-cgi/l/email-protection)";
user.age = 24;
// you can do this
var user = User()
  ..name = "Nicola"
  ..email = "[[email protected]](/cdn-cgi/l/email-protection)"
  ..age = 24;

Spread Operator (...)

// to insert multiple values into a collection.
var list = [1, 2, 3];
var list2 = [0, ...list];
print(list2.length); //Print: 4

Ternary Operator

// condition ? exprIfTrue : exprIfFalse
bool isAvailable;
isAvailable ? orderproduct() : addToFavourite();

Null and Null aware

int x; // The initial value of any object is null
// ?? null aware operator
x ??=6; // ??= assignment operator, which assigns a value of a variable only if that variable is currently null
print(x); //Print: 6
x ??=3;
print(x); // Print: 6 - result is still 6
print(null ?? 10); // Prints: 10. Display the value on the left if it's not null else return the value on the right
Comments