Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Latest commit

 

History

History
68 lines (47 loc) · 1.41 KB

File metadata and controls

68 lines (47 loc) · 1.41 KB

Miscellaneous

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

Ternary Operator

// condition ? exprIfTrue : exprIfFalse
bool isAvailable;

isAvailable ? orderproduct() : addToFavourite();

Spread Operator (...)

// to insert multiple values into a collection.
var list = [1, 2, 3];
var list2 = [0, ...list];

print(list2.length); //Print: 4

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]";
user.age = 24;

// you can do this
var user = User()
  ..name = "Nicola"
  ..email = "[email protected]"
  ..age = 24;

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