Dart by Example: Null-Aware Operators

1. main() {

3. // The ?? operator returns the first expression IFF it is not null
4. var monday = 'doctor';
5. var tuesday;
6. var next = tuesday ?? monday;
7. print('next appointment: $next');

9. // the ??= operator assigns a value IFF it is not null
10. var wednesday;
11. next ??= wednesday;
12. print('next appointment: $next');

14. // the ? operator calls a function IFF the object is not null
15. String thursday;
16. var length = thursday?.length;
17. print('length: $length');

19. }
1. $ dart nullaware.dart
2. next appoinment: doctor
3. next appointment: doctor
4. length: null

by @jryanio | source | license