?? - the null-coalescing operator in Dart

In Dart, the ?? operator is known as the null-coalescing operator. It is used to provide a default value for an expression that may be null.

Here is an example of how the ?? operator can be used in Dart:

int number; // number is null

int result = number ?? 0; // result is 0

In this example, the number variable is initially set to null, and the result variable is set to the value of number if it is not null, or to 0 if it is null. Since number is null, the result variable is set to 0.

The ?? operator is often used as a shorthand way to provide a default value for a variable or expression that may be null. For example:

String name = 'Alice';
String greeting = 'Hello, ' + (name ?? 'there'); // greeting is 'Hello, Alice'

name = null;
greeting = 'Hello, ' + (name ?? 'there'); // greeting is 'Hello, there'

Did this tutorial help a little? How about buy me a cup of coffee?

Buy me a coffee at ko-fi.com

Please feel free to use the comments form below if you have any questions or need more explanation on anything. I do not guarantee a response.

IMPORTANT: You must thoroughy test any instructions on a production-like test environment first before trying anything on production systems. And, make sure it is tested for security, privacy, and safety. See our terms here.