Created
January 25, 2022 01:49
-
-
Save iAmWillShepherd/63b488924db7bbf11841ff70b62ba4ee to your computer and use it in GitHub Desktop.
Syntax for Dart's assign if null operator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main() { | |
var middleName = middle("iAmWillShepherd"); | |
middleName ??= "This assignment won't happen."; | |
print(middleName); // Logs 'S' in the console | |
var noMiddle = middle("No middle exists"); | |
noMiddle ??= "This assignment will happen."; | |
print(noMiddle); // This assignment will happen. | |
} | |
String? middle(String s) { | |
return s.length % 2 == 0 ? null : s.substring(s.length ~/ 2, s.length ~/ 2 + 1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment