xxxxxxxxxx
void main() {
String name; // a nullable string variable
// Using ?? operator to assign a default value if the variable is null
String displayName = name ?? 'Guest';
print(displayName); // Output: Guest
// Using ?. operator to safely access properties or methods of a nullable object
int length = name?.length;
print(length); // Output: null
// Using ??= operator to assign a value only if the variable is null
String tempName;
tempName ??= 'John Doe';
print(tempName); // Output: John Doe
}
xxxxxxxxxx
print(1 ?? 3); // <-- Prints 1.
print(null ?? 12); // <-- Prints 12.
int a; // The initial value of a is null.
a ??= 3;
print(a); // <-- Prints 3.
a ??= 5;
print(a); // <-- Still prints 3.