getStringOrDefault method

String? getStringOrDefault(
  1. K key,
  2. String? defaultValue
)

Returns the string value associated with the given key, or defaultValue if the key does not exist.

Example:

final map = {'name': 'John', 'age': 30};

print(map.getStringOrDefault('name', 'Unknown')); // Output: John

print(map.getStringOrDefault('age', 'N/A')); // Output: 30

print(map.getStringOrDefault('city', 'Not specified')); // Output: Not specified

Implementation

String? getStringOrDefault(K key, String? defaultValue) {
  if (!containsKey(key)) return defaultValue;
  final value = this[key];
  return value?.toString() ?? defaultValue;
}