Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void main() {
- // ? Operator
- Coke myCoke = Coke(volume: 600);
- // this is unsafe as anyone can change volume before printing.
- // for example, myCoke = null
- // then it gonna throw an error.
- print(myCoke.volume); // 600
- // so let's use ? as null check operator to ensure this won't happen
- // means if it is null, if null, stop and return null
- // otherwise continue ".volumne"
- myCoke = null;
- print(myCoke?.volume); // null
- // ?? Operator
- // To set default value
- print(myCoke?.volume ?? '550'); // 550
- myCoke = Coke(volume: 500);
- myCoke.volume = 700;
- print(myCoke?.volume ?? '550'); // 700;
- }
- class Coke {
- // 300 ml
- int volume = 330;
- Coke({ this.volume });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement