Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. void main() {
  2. // ? Operator
  3. Coke myCoke = Coke(volume: 600);
  4. // this is unsafe as anyone can change volume before printing.
  5. // for example, myCoke = null
  6. // then it gonna throw an error.
  7.  
  8. print(myCoke.volume); // 600
  9.  
  10. // so let's use ? as null check operator to ensure this won't happen
  11. // means if it is null, if null, stop and return null
  12. // otherwise continue ".volumne"
  13.  
  14. myCoke = null;
  15. print(myCoke?.volume); // null
  16.  
  17. // ?? Operator
  18. // To set default value
  19. print(myCoke?.volume ?? '550'); // 550
  20.  
  21. myCoke = Coke(volume: 500);
  22. myCoke.volume = 700;
  23. print(myCoke?.volume ?? '550'); // 700;
  24. }
  25.  
  26. class Coke {
  27. // 300 ml
  28. int volume = 330;
  29. Coke({ this.volume });
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement