Created with <3 with dartpad.dev.
Last active
September 21, 2022 14:21
-
-
Save Chralu/bdebc6ad1f765990596c6cc2d321117f to your computer and use it in GitHub Desktop.
Flutter101 - Class getter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Person { | |
static const daysInYear = 365; | |
final String firstname; | |
final String lastname; | |
final double? weight; | |
final DateTime birthdate; | |
Person({ | |
required this.firstname, | |
required this.lastname, | |
required this.birthdate, | |
this.weight, | |
}); | |
// accesseur | |
int get age => (DateTime.now().difference(birthdate).inDays / daysInYear).round(); | |
} | |
void main() { | |
final johnDoe = Person( | |
firstname: "John", | |
lastname: "Doe", | |
birthdate: DateTime(1950, 07, 12), | |
); | |
print(johnDoe.age); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment