What is Null-Safety?

 


I would like to write about one of the most confusing topics when working with Flutter. What is Null-Safety? What is it used for? Where should it be used? I learn much better when I share what I understand and what I have learned by researching. that’s why I love to write.

What is Null-Safety?

A feature in the Dart programming language that makes code safer and more consistent. This minimizes errors caused by null references at runtime and makes the code more predictable.

It was introduced with version 2.12 released in 2020, previous versions of Dart do not have this feature.

In order to use the Null-Safety feature, we need to distinguish between nullable and non-nullable types in Dart programming languages. What does this mean? Variables can or cannot take null values. Let’s look at this in a little more detail.

Nullable (?) Types

When a variable is declared as nullable — which we indicate by putting ? at the end of the variable — it means that the variable can return a null value.
With this usage, we understand that we need to check the value that the variable can return, because it can either take a valid value or return null.

String? nullableString; //A String variable that can return 
//a null or valid value

Non-Nullable (!) Types

We use it to say that the variable defined cannot be null, that we are sure that it will always return a valid value.

With this usage — which we indicate by putting ! at the end of the variable — null values are unacceptable.

String? nullableName = "Selin"; //a variable that has returned a valid value
String nonNullableName = nullableName!; //We use it with an exclamation mark
//to indicate that we are sure that
//it will not return a null value
//to another variable.
print("Merhaba, $nonNullableName");

In this example, we defined the nullableName variable as a nullable String type and initialized it with the value “Selin”. Then, we want to assign this nullable variable to a non-nullable variable. However, in Dart, this will generate an error because we cannot assign a nullable value directly to a non-nullable variable.

The expression nullableName! acts as a pointer to indicate that the variable nullableName is not null. This means that we are indicating to the Dart language that the nullable variable will not actually be null. Thus, we can use this expression to assign the variable nullableName to the variable nonNullableName. In this way, we tell Dart that this value will not be null and we can assign it to a non-nullable variable.

As a result, if the nullableName variable is not null, we get the output “Hello, Selin”. However, if nullableName is null, we get an error at runtime. Therefore, we must be careful when using the ! operator and make sure that the nullable variable is not null.

Null safety makes writing code in Dart safer, more robust and more predictable. If we are going to get an error, being able to predict it in advance makes our work in programming much easier. Especially in large-scale projects like Flutter, we can easily manage debugging and maintenance processes. This gives us developers a significant advantage in creating more robust and reliable applications.

Migration Tool

Before I finish my article, I would like to talk about the Migration tool. As you know, in programming every day something changes, something is introduced, versions are upgraded. Let’s say you have started writing a project and a major update comes along and you need to quickly adapt your project to this update. This is where the migration tool comes in.

We mentioned that the Null-Safety feature came to the Dart language in 2020 with version 2.12. With this tool, you can introduce the Null-Safety feature and all the new features that come with the new version to your project that you started writing before this date, completed or still ongoing.

This makes it easier to migrate your projects or source code from one version to another. In particular, when a major change or update is made, it ensures that existing projects or source code are compatible with the new version.

Going back to the Null-Safety feature, the migration tool automatically examines the existing codebase, distinguishes between nullable and non-nullable types, and applies changes to make the code null-safety compliant. It usually does automatic conversions, but sometimes manual intervention is required in complex or ambiguous cases.

In order to use this tool:

  • We must make sure that the Dart SDK version is the latest version.
  • We must backup our current project just in case. It may be necessary to be able to go back if there is any problem.
  • We run the “dart migrate” command on the terminal screen. This command analyzes the project and generates a migration report suggesting the changes needed to make it null-safety compliant.
  • We carefully review this report and check the migration recommendations.
  • Based on the recommendations in the migration report, we implement the changes that need to be made to the project. This may include performing conversions between nullable and non-nullable types, adding null checks, and other necessary changes.
  • After the changes are implemented, we test our project and make sure it works correctly. Testing is very important, unexpected situations can occur, errors can be received.
  • If we are sure that our project is working correctly after all changes have been made, we are done with the migration process.


In conclusion, the Null-Safety feature makes writing code in Dart safer and gives us developers a more robust and consistent programming experience. I hope I have been useful.

Good luck to you.

Selin.


Hiç yorum yok: