Null aware operator are used in almost every programming language to check whether the given variable value is Null. The keyword for Null in programming language Dart is null. Null means a variable which has no values assign ever and the variable is initialize with nothing like. The most common use of Null aware operator is when developer wants to parse JSON data from server and after parsing JSON, user can check whether the JSON is empty or not using IF-Else condition. So in this tutorial we would learn about Null Aware Operator in Dart Flutter with Example.
Contents in this project Null Aware Operator in Dart Flutter Example:
Dart comes with 3 Null aware operators, Here are they :-
1. The ?. Operator :- The ?. Null aware operators can easily check the given JSON complete object value is Null or not by using simple syntax.
1 |
this.JSON_Obj = data?.name ; |
This condition would simply check whether the JSON object has a data named as name or not.
2. The ?? Operator :- The ?? Null aware operator can also check the given object has Null value and we can also pass default value using the ?? operator.
1 2 3 |
// If JSON_Name is null, defaults Will be Pankaj this.JSON_Name = JSON_Name ?? 'Pankaj'; |
3. The ??= Operator :- The ??= Null Aware operator can check whether the given value variable is Null or not. If the variable is Null then it will assign a new value to it and if the variable is Not Null then it will returns us As it is.
1 2 |
int a = 10 a ??= 7; |