getNumOrDefault method
Returns the numeric value associated with the given key
,
or defaultValue
if the key does not exist or cannot be converted to num
.
Example:
final map = {'price': '19.99', 'discount': 5};
print(map.getNumOrDefault('price', 0)); // Output: 19.99
print(map.getNumOrDefault('discount', 0)); // Output: 5
print(map.getNumOrDefault('tax', 2.5)); // Output: 2.5
Implementation
num? getNumOrDefault(K key, num? defaultValue) {
if (!containsKey(key)) return defaultValue;
final value = this[key];
if (value is num) return value;
if (value is String) return value.toNum() ?? defaultValue;
return defaultValue;
}