What it is, how it works and how to implement it in your code

Hello, are you ready to learn about one of the most anticipated features of Dart 3? It's null safety, which allows you to write more robust code and avoid common errors related to null values. In this post, I will explain what this feature is and how you can take advantage of it in your projects.

What is null safety?

Null safety is a way to ensure that the variables you declare in your code cannot contain a null value unless you explicitly specify it. This way, you avoid the risk of raising exceptions such as NullPointerException or ArgumentError when trying to access properties or methods of a null object.

How does null safety work in Dart?

Dart 3 introduces the concept of "non-nullable by default", which means that all variables you declare in your code are considered non-nullable unless you add the ? operator at the end of the type. For example:

// This variable is non-nullable
// and must be initialised with a value.
int age;

// This variable is nullable, and can contain null
int? height;

// This variable is non-nullable
// and is initialized with a default value.
int weight = 70;

If you try to assign a null value to a non-nullable variable, the Dart parser will throw an error. Similarly, if you try to access a property or method of a nullable variable without first checking if it is null, you will also get an error. To avoid these errors, you can use the ! operator to assert that a nullable variable is not null, or the ?? operator to provide an alternative value in case it is null. For example:

// We assert that height is not null
// and access its floor property.
int meters = height!.floor();

// Provide an alternative value
// in case name is null
String greeting = "Hello ${name ?? 'guest'}";

How can I enable null safety in my project?

To enable null security in your project, you need to make sure you have Dart version 2.12 or later installed. Then, you must update the pubspec.yaml file with the following line:

    sdk: '>=2.12.0 <3.0.0'.

Next, you must run the following commands to update the dependencies and migrate your code:

$ dart pub upgrade --null-safety
$ dart pub get
$ dart migrate

The dart migrates command will open a web tool that will help you review and apply the necessary changes to adapt your code to null safety.

What are the benefits of null safety?

Null security has many benefits for developers using Dart. Some of them are:

  • It improves code quality by avoiding errors related to null values.

  • It facilitates reasoning about program flow by eliminating ambiguity between non-nullable types and potentially nullable types.

  • Increases productivity by reducing the number of checks and assertions required to work with variables.

  • Improves performance by optimising memory usage and avoiding unnecessary operations.

Null safety is a very powerful and important feature that makes Dart a safer, more expressive and efficient language. I encourage you to try it in your projects and enjoy its advantages. If you have any questions or comments about this feature, feel free to leave them below or contact me via social media. See you soon!