Flutter for Beginners
上QQ阅读APP看书,第一时间看更新

Null-safe and null-aware operators

Following the trend on modern OOP languages, Dart provides a null-safe syntax that evaluates and returns an expression according to its null/non-null value.

The evaluation works in the following way: if expression1 is non-null, it returns its value; otherwise, it evaluates and returns the value of expression2expression1 ?? expression2.

In addition to the common assignment operator, =, and the ones listed in the corresponding operators, Dart also provides a combination between the assignment and the null-aware expression; that is, the ??= operator, which assigns a value to a variable only if its current value is null.

Dart also provides a null-aware access operator, ?., which prevents accessing null object members.