daysInMonth method

int? daysInMonth(
  1. int monthNum,
  2. int year
)

"Given a month number and a year, return the number of days in that month."

The first line of the function is a comment. Comments are ignored by the Dart compiler

Args: monthNum (int): The month number, 0 for January, 1 for February, and so on. year (int): The year to check.

Implementation

int? daysInMonth(final int monthNum, final int year) {
  if (monthNum > 12) {
    return null;
  }
  List<int> monthLength = List.filled(12, 0);
  monthLength[0] = 31;
  monthLength[2] = 31;
  monthLength[4] = 31;
  monthLength[6] = 31;
  monthLength[7] = 31;
  monthLength[9] = 31;
  monthLength[11] = 31;
  monthLength[3] = 30;
  monthLength[8] = 30;
  monthLength[5] = 30;
  monthLength[10] = 30;

  if (leapYear(year) == true)
    monthLength[1] = 29;
  else
    monthLength[1] = 28;

  return monthLength[monthNum - 1];
}