Dart by Example: Null-Aware Operators

  1. main() {
  2. // The ?? operator returns the first expression IFF it is not null
  3. var monday = 'doctor';
  4. var tuesday;
  5. var next = tuesday ?? monday;
  6. print('next appointment: $next');
  7. // the ??= operator assigns a value IFF it is not null
  8. var wednesday;
  9. next ??= wednesday;
  10. print('next appointment: $next');
  11. // the ? operator calls a function IFF the object is not null
  12. String thursday;
  13. var length = thursday?.length;
  14. print('length: $length');
  15. }
  16.  
  1. $ dart nullaware.dart
  2. next appoinment: doctor
  3. next appointment: doctor
  4. length: null

by @jryanio | source | license