Skip to content

Instantly share code, notes, and snippets.

@icgoogo
Last active October 3, 2023 10:05
Show Gist options
  • Save icgoogo/78c3ef9cef7189ca494b0bf19e3a222e to your computer and use it in GitHub Desktop.
Save icgoogo/78c3ef9cef7189ca494b0bf19e3a222e to your computer and use it in GitHub Desktop.
Dart snippets for simplified if and switch condition
import 'dart:math';
///second simplified example
String get spelledOut2 {
switch(Random().nextInt(4)) {
case 0:
return "Zero";
case 1:
return "One";
case 2:
return "Two";
case 3:
return "Three";
default:
return "Out of range";
}
}
void main() {
///first complicated example
String firstComment;
if (Random().nextInt(4)%2==0) {
firstComment = "event";
} else {
firstComment = "odd";
}
print(firstComment);
///first simplified example
String comment = Random().nextInt(4)%2 == 0 ? "even" : "odd";
print(comment);
///second complicated example
late var spelledOut;
switch(Random().nextInt(4)) {
case 0:
spelledOut = "Zero";
case 1:
spelledOut = "One";
case 2:
spelledOut = "Two";
case 3:
spelledOut = "Three";
default:
spelledOut = "Out of range";
}
print(spelledOut);
print(spelledOut2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment