Open In App

Dart – Null Aware Operators

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 that you will get 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 which has no values assign ever and the variable is initialized with nothing like. 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 few Null-aware operators that are explained.



1. Default Operator: ??

Example 1:




void main(){
   
  // In this we have defined the value of variable b.
 var b = "GeeksforGeeks";
  String a = b ?? 'Hello';
  print(a);
   
  // In this we have not defined the value of variable c.
  var c;
  String d = c ?? 'hello';
  print(d);
}

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 :




void main() {
   var code;
   code = code ?? "Java";
   print(code);
 
   var companyName = "Microsoft";
   companyName = companyName ?? "Google";
   print(companyName);
}

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.

2. Operational spread operator:  …?

Example 1:




void main(){
  List<int> lowerNumber = [1,2,3,4,5];
  List<int> upperNumbers = [6,8,9,0];
  lowerNumber = [...lowerNumber,...?upperNumbers];
  print('numbers are ${lowerNumber}');
   
  List<int> listNull;
  lowerNumber = [...lowerNumber, ...?listNull];
  print('new list are ${lowerNumber}');
}

Output: 

Explanation:

Example 2:




void main(){
 List<Friend> friendA = [
   Friend(name: 'Sara', age: 12),
   Friend(name: 'Jenny', age: 17)
 ];
  List<Friend> friendB;
  List<Friend> myFriends = [...friendA, ...?friendB, Friend(name:  'Julia', age: 15)];
  myFriends.forEach((friend) => print(friend.name));
}
class Friend{
  String name;
  int age;
  Friend({this.name, this.age});
}

 
 Output: 

 


Article Tags :