maxOf static method

num? maxOf(
  1. num? a,
  2. num? b
)

Returns the maximum of two nullable numbers.

If both a and b are null, returns null. If only one of a or b is null, returns the non-null number. If both are non-null, returns the larger of the two.

Example:

NumberUtils.maxOf(10, 5); // Returns 10
NumberUtils.maxOf(null, 5); // Returns 5
NumberUtils.maxOf(10, null); // Returns 10
NumberUtils.maxOf(null, null); // Returns null

Implementation

static num? maxOf(num? a, num? b) {
  // If both numbers are null, return null
  if (a == null && b == null) {
    return null;
  }

  // If the first number is null, return the second number
  if (a == null) {
    return b;
  }

  // If the second number is null, return the first number
  if (b == null) {
    return a;
  }

  // Return the larger of the two numbers
  return a > b ? a : b;
}