![]() |
VOOZH | about |
Null-aware operators in Dart allow you to make computations based on whether or not a value is null. It’s shorthand for longer expressions. A null-aware operator is a nice tool for making nullable types usable in Dart instead of throwing an error. These operators are used in fullback in combination so that you will get a value at the end, but not null. Null-aware operators are used in almost every programming language to check whether the given variable value is Null. The keyword for Null in the programming language Dart is null. Null means a variable that has no values assigned ever, and the variable is initialized with nothing. The most common use of the null-aware operator is when a developer wants to parse JSON data from the server, and after parsing JSON, the user can check whether the JSON is empty or not using the IF-Else condition.
Here are a few Null-aware operators that are explained.
Example 1:
Output:
GeeksforGeeks
hello
Explanation: In the above example, we have two parts. In the first part value of variable b is not null. In the second part value of the variable c is null. In the first part, since the variable b is not null, the ?? operator will return the assigned value, i.e., GeeksforGeeks,and in the second part, the variable c is null, hence the second value will be returned from the ?? operator, i.e hello.
Example 2 :
Output:
Java
Microsoft
Explanation: In the above example, we declared two variables, and one of them is of null value, and the other is not null and contains a string value. We are using the ?? operator when reassigning values to those variables. In the first variable, since the variable code is null, the ?? operator will return the second value, i.e., Java, and in the second case, the variable companyName is not null, hence the first value will be returned from the ??operator, i.e, Microsoft.
Example 1:
Output:
Explanation:
Error: An expression whose value can be 'null' must be null-checked before it can be dereferenced.
lowerNumber = [...lowerNumber, ...listNull];
^
Example 2:
Output: