Skip to main content

The null value

In Dart 2.0, the Dart team added null safety to the Dart language. When a variable is null it is because it does not have any reference to any object, or we can say that it does not have any value.

When we create a variable by default, it cannot have null values. If we try to compile the following code, it will fail:

String myName = null;

If we want to create a variable with null support, we must add a question mark like this:

String? myName = null;

When should we use a variable with null support?

We should only create a variable with null support when we are 100% sure that it can be null this will help our IDE to understand if we are trying to assign null to a non-null variable, so we won't be able to compile the app until these errors are fixed.