4.4. Advanced Dart Concepts: Null Safety

Página 42

To begin to understand advanced Dart concepts, it is important to first understand what Null safety is. Null safety is a way to ensure that your code doesn't have null pointer errors, which are common errors in many programming languages, including Dart. With Null safety, you can effectively prevent these errors, making your code safer and more efficient.

Dart 2.12 introduced Null safety as one of its key features, which means that data types cannot be null by default. For example, if you declare a variable as a String, it cannot be null. If you want it to be nullable, you must declare it as a String?.

To understand better, let's consider an example. Suppose you have a User object with a name field. If you try to access user.name and user is null, you will get a Null Pointer Exception error. But with Null safety, the Dart compiler will warn you that user may be null and that you need to check this before accessing the name field.

So how can you verify this? The most common way is using the ? operator. before accessing the field. Then you would do user?.name. If user is null, user?.name will also be null, and no exception will be thrown.

Another way is using the !! operator. If user is null and you do user!!.name, an exception will be thrown. This is useful when you are absolutely certain that user is not null. If you're wrong, you'll get an exception, which is a good way to find bugs in your code.

In addition, Dart also introduced the concept of non-null types and potentially null types. A non-null type is a type that cannot be null. For example, int is a non-null type, because you cannot assign null to it. On the other hand, int? is a potentially null type, because you can assign null to it.

With Null safety, you can also use the ?? operator. to provide a default value for a variable that may be null. For example, you can do user?.name ?? 'Guest' to use 'Guest' as the default name if user is null or if user.name is null.

Another useful feature introduced with Null safety is the ?.. operator for calling methods or accessing fields on an object that might be null. For example, you can do user?..logout() to call the logout method on user only if user is not null.

In short, Null safety is a powerful Dart feature that helps make your code safer and cleaner by preventing common null pointer errors. It does this through a combination of non-null and potentially null types, and operators such as ?, !!, ?? and ?... With these features, you can write code that handles null safely and effectively, making your code more robust and less prone to bugs.

Therefore, when creating applications from scratch to advanced using Flutter and Dart, it is essential to have a good understanding of Null safety and how to use it in your code. Not only will this help you avoid common mistakes, but it will also make your code easier to understand and maintain.

With Null safety, you can ensure that your Dart code is robust, secure, and efficient. And that's essential to building high-quality apps with Flutter and Dart.

Now answer the exercise about the content:

What is Null Safety in the context of the Dart programming language and how can it be used?

You are right! Congratulations, now go to the next page

You missed! Try again.

Next page of the Free Ebook:

434.5. Advanced Dart Concepts: Asynchronous Programming

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text