Created with <3 with dartpad.dev.
Created
September 21, 2022 14:14
-
-
Save Chralu/8ecb0c6498d1965182dde2915861e515 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
const daysInYear = 365; | |
class Person { | |
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