Skip to content

Instantly share code, notes, and snippets.

@topex-psy
Last active April 4, 2023 03:30
Show Gist options
  • Save topex-psy/830d8a7490e6e04650559b15fc70fa7a to your computer and use it in GitHub Desktop.
Save topex-psy/830d8a7490e6e04650559b15fc70fa7a to your computer and use it in GitHub Desktop.
Dart 5. Enum & Switch Case

Dart 5. Enum & Switch Case

Created with <3 with dartpad.dev.

void main() {
var spacecraft = Spacecraft("Roket 101", type: SpacecraftType.lander, launched: DateTime.tryParse("2022-01-02"));
spacecraft.launchDate = "2021-01-02";
spacecraft.describe();
}
enum SpacecraftType {
flyby,
orbitter,
lander,
commNav
}
class Spacecraft {
// properties
String name;
DateTime? launched;
SpacecraftType type;
// getter
int? get launchYear => launched?.year;
String get spacecraftType {
switch (type) {
case SpacecraftType.flyby: return "Flyby";
case SpacecraftType.orbitter: return "Orbitter";
default: return "Unknown";
}
}
// setter
set launchDate(String date) {
launched = DateTime.tryParse(date);
}
// constructor
Spacecraft(this.name, {required this.type, this.launched}) {
// di sini inisialisasi
}
//method
void describe() {
print('$spacecraftType spacecraft "$name" ${launched == null
? "belum meroket"
: "sudah meroket pada $launchYear"}');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment