Skip to content

Instantly share code, notes, and snippets.

@johnpryan
Last active April 24, 2023 23:23
Show Gist options
  • Save johnpryan/349650f3936c79363f81ee0f1d6e8a12 to your computer and use it in GitHub Desktop.
Save johnpryan/349650f3936c79363f81ee0f1d6e8a12 to your computer and use it in GitHub Desktop.
Patterns example
class Drink {
int ounces;
Drink(this.ounces);
}
class Coffee extends Drink {
final bool decaf;
Coffee(int ounces, {this.decaf = false}) : super(ounces);
}
class SportsDrink extends Drink {
final String? flavor;
SportsDrink(int ounces, this.flavor) : super(ounces);
}
void main() {
var drinks = <Drink>[
Coffee(12, decaf: true),
SportsDrink(16, null),
];
for (var drink in drinks) {
switch(drink) {
case Coffee(:var decaf):
print('coffee (decaf: $decaf)');
case SportsDrink(:final flavor?):
print('coffee (flavor: $flavor)');
default:
print("no match");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment