I built datepicker function. when the date selected then should calculate the age and should display date and age. The date display correctly but age not show.
code
ElevatedButton(
onPressed: () {
send();
calAge();
},
child: const Text("submit"),
),
Text(selectedDate == null ? "" : "$age"),
void calAge(DateTime birthDate) {
DateTime currentDate = DateTime.now();
int age = currentDate.year - birthDate.year;
int month1 = currentDate.month;
int month2 = birthDate.month;
if (month2 > month1) {
age--;
} else if (month1 == month2) {
int day1 = currentDate.day;
int day2 = birthDate.day;
if (day2 > day1) {
age--;
}
}
return age;
}