Summary: In this tutorial, you’ll learn about various Dart null-aware operators to handle null values.
Introduction to Dart null-aware operators #
To deal with null values, Dart uses flow analysis and type promotion. In addition, it provides you with various null-aware operators:
| Operator | Meaning |
|---|---|
?? | If-null operator |
??= | Null-aware assignment operator |
?. | Null-aware access & method invocation operator |
! | Null assertion operator |
?.. | Null-aware cascade operator |
?[] | Null-aware index operator |
...? | Null-aware spread operator |
The if-null operator (??) #
The following example uses an if statement to check if a variable is null and assign an error message if it is:
void main() {
String? input;
String message;
if (input != null) {
message = input;
} else {
message = 'Error';
}
print(message);
}Code language: Dart (dart)Output:
ErrorCode language: Dart (dart)To make this code more precise, you can use the if-null operator:
value ?? value_if_nullCode language: Dart (dart)The ?? operator returns the value if the value is not null. Otherwise, it returns the value_if_null. By using the if-null operator, you can turn the example above into a more concise code:
void main() {
String? input;
String message = input ?? 'Error';
print(message);
}Code language: Dart (dart)Output:
ErrorCode language: Dart (dart)The null-aware assignment operator (??=) #
Sometimes, you have a single nullable variable:
String? input;Code language: Dart (dart)And you want to assign it a value if it is null, like this:
input = input ?? 'Error';Code language: Dart (dart)This code works well. However, it is quite redundant to reference the input variable on both sides of the expression.
To avoid this, you can use the null-aware assignment operator:
input ??= 'Error';Code language: Dart (dart)In this syntax, if the input is null, it is assigned the string 'Error'.
The null-aware access operator (?.) #
Suppose you have a nullable string variable called input like this:
String? input;Code language: Dart (dart)To access the property and method of the String type, you need to check if it’s not null:
void main() {
String? input;
if (input != null) {
print(input.length);
print(input.toLowerCase());
}
}Code language: Dart (dart)To avoid using the if statement, you can use the null-aware access operator (?.):
objectName?.propertyCode language: Dart (dart)The null-aware access operator returns null if the objectName is null. For example:
void main() {
String? input;
print(input?.length); // null
print(input?.toLowerCase()); // null
}Code language: Dart (dart)Null assertion operator (!) #
The following code causes a compile-time error:
bool? isTextFile(String? filename) {
if (filename != null) {
return filename.endsWith('.txt') ? true : false;
}
return null;
}
void main() {
bool result = isTextFile('readme.txt');
print(result);
}Code language: Dart (dart)Error:
A value of type 'bool?' can't be assigned to a variable of type 'bool'.Code language: Dart (dart)The isTextFile() function returns a value of the bool? type not bool. Therefore, you cannot assign the result of the isTextFile() function to the result variable.
Since you’re sure that the isTextFile('readme.txt') doesn’t return a null value, you can use the null assertion operator (!) like this:
bool result = isTextFile('readme.txt')!;Code language: Dart (dart)The program will look like the following
bool? isTextFile(String? filename) {
if (filename != null) {
return filename.endsWith('.txt') ? true : false;
}
return null;
}
void main() {
bool result = isTextFile('readme.txt')!;
print(result);
}Code language: Dart (dart)Null-aware index operator #
The null-aware index operator ?[] allows you to access an element of a list when the list might be null. For example:
void main() {
List? scores = [1, 2, 3, 4, 5];
// somewhere in the code
scores = null;
print(scores?[3]); // null
}Code language: Dart (dart)In this example, we use the null-aware index operator to access the 4th element of the scores list. Because the scores list is null, the ?[] returns a null value.
Summary #
- Use null-aware operators to deal with null values.