getDoubleOrDefault method
Returns the double value associated with the given key
,
or defaultValue
if the key does not exist or cannot be converted to double
.
Example:
final map = {'rating': '4.5', 'average': 3.2};
print(map.getDoubleOrDefault('rating', 0.0)); // Output: 4.5
print(map.getDoubleOrDefault('average', 0.0)); // Output: 3.2
print(map.getDoubleOrDefault('score', 1.0)); // Output: 1.0
Implementation
double? getDoubleOrDefault(K key, double? defaultValue) {
if (!containsKey(key)) return defaultValue;
final value = this[key];
if (value is double) return value;
if (value is num) return value.toDouble();
if (value is String) return value.toDouble() ?? defaultValue;
return defaultValue;
}