Get immediate access to this and over 1,500+ other videos and books.

Boost your skills with a raywenderlich.com Beginner subscription. With over 60+ video courses and our core foundational programming books bundled in one subscription, it’s simply the best investment you can make in your development career.

Understand Null Safety

This video was last updated on Apr 26 2022

Learn about one of the most important values in Dart - a null value.

Contributors

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

You can unlock the rest of this video course, and our entire catalogue of books and videos, with a raywenderlich.com Professional subscription.

In 2021, Dart was upgraded with a huge feature that would affect every single line of Dart. And that was null safety. It was a huge upgrade and it really comes into play when working with collections. So what is null safety? What is being null? Null means the absence of a value. Lets say you are teacher, writing some code to make correcting grades easier for you. You may create a quiz but how do you know that a student has actually taken the quiz? Well, you could set the quiz value to zero, but that’s an actual grade. So you may next choose a negative number like negative one but what if your data store doesn’t accept negative numbers? After all, you can’t actually have a negative grade? You could solve this problem in a variety of ways but with null, it’s already solved for you.

var grades = List<int?>.empty(growable: true);
grades.add(100);
grades.add(null);
grades.add(84);
var total = 0;
var firstTest = grades[0];
total += firstTest;
if (firstTest != null) {
    total += firstTest;
} 
total += grades[1] ?? 0;
total += grades[2]!; 
var average = total / grades.length;
print('The average is $average');

Reviews

Comments