maxOf static method
Returns the maximum of a and b, handling null values.
If both are null, returns null. If only one 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
@useResult
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;
}