Learning null Safety in Dart:
understanding why we need null Safety; Null means nothing is there. so handle the programme at any point in time if there is a value which is null what should be performed?
Sound NullSafety:
By sound you might is understand that here you may know when the value will be null; so for that flow, we use SOUND Null Safety
int i = 0;
i= null; ❌
// here i is 0; but what if somepoint it becomes null;
// then it will throw an error
// so we need to add a check for null
<!-- down arrow unicode -->
int❓ j = 1;
//^ this is a nullable variable; so in future it can be null; and wount' throw error.
// make any value nullable by adding ❓after the variable type.
j = null; ✅
List<String> names = ['Ram', 'Shyam', 'Hari'];
names[0] = null; ❌
List<String?> names2 = ['Ram', 'Shyam', null];
names2[0] = null; ✅
List<String>? names3 = ['Ram', 'Shyam', 'Hari'];
names3 = null ✅
// for making a list nullable we need to add ? after the list type here it is String.
List<String?>? names4 = [null, null, "Ram"]; ✅
// Here List as well as list Type is also nullable.
Null Assign:
Null Assign means if the value is null Assign this value
int? age = 12;
age = null;
int RealAge = age ?? 18;
// adding this ❓❓ after the variables (age) makes us sure that if any case the value of age is null the RealAge will be 18;
Null Check
Null check is a process of checking if the value is null or not, and perform tasks accordingly.
List<String>? names5 = null;
print(names5?.length);
// it is shortcut for
if(names5 != null) {
print(names5.length);
} else {
print("null");
}
print(Person?.skills?.length);✅
.❓.❓.❓
// it will only print the length of skills if the person is not null and if the skills are not null.
I will explain Objects; Classes in the next blog.
Share with me if anything is missing or you don't understand; don't shy 🥺
ping me on Twitter 🕊️ @Abhayprajapati_
Check the code for today here.
Repo
🤝🏾Connect me on:
Twitter: 🕊️@Abhayprajapati_
Github: 🐧@theabhayprajapati
Top comments (0)