Open In App

Dart – Null Aware Operators

Improve
Improve
Like Article
Like
Save
Share
Report

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: ??

  • We use ?? when you want to evaluate and return an expression if another expression resolves to null.
  • It is also called the if-null operator and coalescing operator.
  • The null-aware operator is ??, which returns the expression on its left unless that expression’s value is null. In which case it’s null it returns the expression on its right:

Example 1:

Dart




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 :

Dart




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:  …?

  • This operator was introduced in Dart version 2.3.
  • Placing … before an expression, inserts a list into another only if it’s not null.
  • It helps add multiple values to our collection like List, Map, and Set.
  • It is also called a Null check operator.

Example 1:

Dart




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:

  • In the first line of output, we get the appended list.
  • Now we see the value of listNull is not assigned i.e it’s null.
  • In dart, if we don’t use (…?) operator we get an error because the value of listNull is null and we cannot print the desired output which you can see below –

  • To remove this error we have used the (…?) operator so that we don’t get an error and get desired output.
  • So in the second line, we got a new list without causing an error.

Example 2:

Dart




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: 

 



Last Updated : 28 Jan, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads