Flutter / Basic / Null Aware Operators
NUll Checking
-
Steps
1. ?. operator
null checking
Shorter null checkingUser user = new User.fromResponse(response); // null check if (user != null) { this.userAge = user.age; } User user = new User.fromResponse(response); // delightfully shorter null check this.userAge = user?.age; 2. The ?? operator
you can use the double question mark (??) to assign a "fallback" or default value.
User user = new User.fromResponse(response); // If user.age is null, defaults to 18 this.userAge = user.age ?? 18; <1> // etc. 3. The ??= operator
int x = 5 x ??= 3; In the second line, x will not be assigned 3, because it already has a value.