Skip to content

Instantly share code, notes, and snippets.

@mihalycsaba
Last active May 5, 2020 15:10
Show Gist options
  • Save mihalycsaba/54b77db084b5fb476197b9d2eca2cc4a to your computer and use it in GitHub Desktop.
Save mihalycsaba/54b77db084b5fb476197b9d2eca2cc4a to your computer and use it in GitHub Desktop.
class Person {
DateTime birthday;
String name;
final int days = 365;
Person(this.name, {this.birthday});
set age(double years) {
var d = new DateTime.now();
var dur = new Duration(days: (days * years).toInt());
d = d.subtract(dur);
birthday = d;
}
double get age {
var d = new DateTime.now();
return d.difference(birthday).inDays / days;
}
double myage() {
var d = new DateTime.now();
return d.difference(birthday).inDays / days;
}
}
main() {
var p = new Person("Foo");
print(p.name);
p.age = 18.0;
print(p.age);
print(p.myage()); // () required because it is not a real getter
var o = new Person("Bar", birthday: new DateTime.now());
print(o.name);
print(o.birthday);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment