Skip to content

Instantly share code, notes, and snippets.

@minikin
Created May 21, 2019 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save minikin/9ed0ec3f00f9691fab14563f1d864515 to your computer and use it in GitHub Desktop.
Save minikin/9ed0ec3f00f9691fab14563f1d864515 to your computer and use it in GitHub Desktop.
Switch on a Set using List literals as cases in Dart.
// Switch on a Set using List literals as cases in Dart.
// Can be really useful to avoid many if/else statements.
enum Direction { up, down, left, right }
class MapTile {
var directions = Set<Direction>();
String render() {
switch (directions.toList()) {
case [Direction.up, Direction.down]:
return 'map_tile_vertical.png';
case [Direction.left, Direction.right]:
return 'map_tile_horizontal.png';
default:
return 'default.png';
}
}
}
void main() {
final mapTile = MapTile();
mapTile.directions = const {Direction.up};
print(mapTile.render());
}
@minikin
Copy link
Author

minikin commented May 21, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment