Table of Contents
Null Safety Principles for Variables in Dart
Learning Null Safety Principles for Variables in dart is one of the most important topics in Mobile App Development. We will start from basics by understanding its effects and later on will learn to fix it. First of all, the term ‘Null’ refers to the absence of data or emptiness. If there is no value in the variable then it is called null. To avoid null, we apply null safety principles. A Mobile App Developer needs to understand them and fix null exceptions because he has to deal with this phenomenon during Mobile App Development.
What happens in the case of Null?
It is not an ordinary exception and if we fail to counter, our app can even crash due to a null exception. It can be in variables, functions response, and objects, etc. Null is a reserved word and we must not use it elsewhere. It can also be compared to a virus. We can use both small and capital cases i.e. ‘null’ or ‘Null’.
Null Safety Principles – types of variables:
Based on the null safety principles, there are two types of variables i.e.:
Nullable variables
Non-nullable variables
Let’s explore one by one.
Nullable variables
Variables that can contain null values are called nullable variables. For example, inferred variables can contain null values and we cannot convert inferred variables to non-nullable. Following is the example of the inferred variable with null:
void main() {
var name = null;
var marks = null;
}
If we do not give value to inferred variable then null will also come in it.
Non-nullable variables
Variables that cannot contain null values are called non-nullable variables. By default, all statically typed variables are non-nullable variables meaning that null cannot come in them. Moreover, in statically typed variables we have to give value so there is no question of null in it. For example, it cannot be as under:-
void main() {
String name = null;
int marks = null;
}
How to make statically typed variables nullable
We can make the above explained statically typed variables nullable. For this purpose we need to add a question mark (?) after its type like:
void main() {
String? name = null;
int? marks = null;
}
Solution of Null
So from the above details, we understood that it is just like a virus and can even crash our App. How to deal with this virus or what is its anti-virus? So there are null aware operators to deal with nullable variables which not only aware of us but also fix null.
Null aware operators
Let’s explore them:
Operator No1: Two question marks (??)
To understand let’s start with an example:
void main() {
String? name;
var bb;
print(name ?? “Ali”);
}
As in the above example, there is a null in the name. So in print, we used the null aware operator of ??. Now as there is null in the name, Ali will be printed. Actually, in this scenario replacement is made. If there is no null in our variable like:
void main() {
String? name = “usman”;
var bb;
print(name ?? “Ali”);
}
Then usman will be printed. So it’s a good way to tackle null in print.
Summary:
In case of a non-null scenario, the value will be printed. In case there is null then after ?? value will be printed. So the statement after ?? is implemented when there is null. If there is no null then the actual value prints.
Operator No.2: Two question marks with equal (??=)
It is about assignment. We can use it while making variables and not necessary to use it in print. It works in nullable variables. For example
void main() {
int? no1;
int no2;
no2 = no1 ??= 10;
print (no1);
}
Now as there is null in no1 to avoid null we used ??= and in this case 10 will be printed. If there is no null in no1 like:
void main() {
int? no1 = 20;
int no2;
no2 = no1 ??= 10;
print (no1);
}
Then 20 will be printed. Try yourself in Dart Pad.
We need to practice it as it will help us to tackle null and to be a good Mobile App Developer!!