Skip to content

Instantly share code, notes, and snippets.

@topex-psy
Last active March 27, 2023 04:00
Show Gist options
  • Save topex-psy/468522de6bc586ae9a68c8e90ef6b4a7 to your computer and use it in GitHub Desktop.
Save topex-psy/468522de6bc586ae9a68c8e90ef6b4a7 to your computer and use it in GitHub Desktop.
Dart 4. Function, Class & Method

Dart 4. Function, Class & Method

Created with <3 with dartpad.dev.

void main() {
double kuadrat(double n) {
return n * n;
}
print(kuadrat(20));
var spacecraft = Spacecraft("Roket 101", launched: DateTime.tryParse("2022-01-02"));
spacecraft.launchDate = "2021-01-02";
spacecraft.describe();
}
class Spacecraft {
// properties
String name;
DateTime? launched;
// getter
int? get launchYear => launched?.year;
// setter
set launchDate(String date) {
launched = DateTime.tryParse(date);
}
// constructor
Spacecraft(this.name, {this.launched}) {
// di sini inisialisasi
}
//method
void describe() {
print('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