Skip to content

Instantly share code, notes, and snippets.

@iAmWillShepherd
Created January 25, 2022 01:49
Show Gist options
  • Save iAmWillShepherd/63b488924db7bbf11841ff70b62ba4ee to your computer and use it in GitHub Desktop.
Save iAmWillShepherd/63b488924db7bbf11841ff70b62ba4ee to your computer and use it in GitHub Desktop.
Syntax for Dart's assign if null operator
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