convertAllValuesToString method

Map<String, dynamic> convertAllValuesToString()

Returns a new map with string values only.

The method iterates over each key-value pair in the original map. If the value is of type Map, it recursively converts its values to strings. If the value is of type DateTime, it converts it to a string representation in ISO 8601 format. Otherwise, it converts the value to a string using toString().

Returns a new map with the same keys but values converted to strings.

Example:

Map<dynamic, dynamic> originalMap = {
  'name': 'Alice',
  'age': 25,
  'birthday': DateTime(1990, 1, 1),
};

Map<dynamic, dynamic> stringMap = originalMap.convertAllValuesToString();
print(stringMap); // Output: {name: Alice, age: 25, birthday: 1990-01-01T00:00:00.000}

Implementation

Map<String, dynamic> convertAllValuesToString() {
  var result = <String, dynamic>{};

  this.forEach((key, value) {
    if (value is Map) {
      result[key.toString()] = (value).convertAllValuesToString();
    } else if (value is DateTime) {
      result[key.toString()] = value.toIso8601String();
    } else {
      result[key.toString()] = "$value";
    }
  });

  return result;
}