intx;// The initial value of any object is null// ?? null aware operatorx??=6;// ??= assignment operator, which assigns a value of a variable only if that variable is currently nullprint(x);//Print: 6x??=3;print(x);// Print: 6 - result is still 6print(null??10);// Prints: 10. Display the value on the left if it's not null else return the value on the right
// to insert multiple values into a collection.varlist=[1,2,3];varlist2=[0,...list];print(list2.length);//Print: 4
Cascade notation (..)
// allows you to make a sequence of operations on the same object// rather than doing thisvaruser=User();user.name="Nicola";user.email="nicola@g.c";user.age=24;// you can do thisvaruser=User()..name="Nicola"..email="nicola@g.c"..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 expressionuserObject?.userName?.toString()// The preceeding code returns null and never calls toString() if either userObject or userObject.userName is null
enum in dart
defination:Anenum(shortfor"enumeration")isaspecialdatatypethatenablesavariabletobeasetofpredefinedconstants.Enumsareusedtodefinevariablesthatcanonlytakeoneoutofasmallsetofpossiblevalues.Theyhelpmakecodemorereadableandlesserror-pronebyprovidingmeaningfulnamestothesesetsofvalues.// Define the enumenumTrafficLight{red,yellow,green}// A function that prints a message based on the traffic light statevoidprintTrafficLightMessage(TrafficLightlight){switch(light){caseTrafficLight.red:print('Stop!');break;caseTrafficLight.yellow:print('Get ready...');break;caseTrafficLight.green:print('Go!');break;}}voidmain(){// Example usage of the enumTrafficLightcurrentLight=TrafficLight.green;// Print the message for the current traffic light stateprintTrafficLightMessage(currentLight);}