applyUpdate function

dynamic applyUpdate(
  1. dynamic value,
  2. String updates
)

Apply update transform to merge JSON objects

Merges update data into an existing JSON object. The update data overwrites existing keys and adds new keys. Non-object values are returned unchanged.

Parameters

  • value - The base object to update (must be a Map)
  • updates - JSON string containing update data

Returns

  • Merged object if value is a Map and updates are valid JSON
  • Original value if value is not a Map or updates are invalid

Examples

final obj = {'name': 'Alice', 'age': 30};
applyUpdate(obj, '{"age": 31}');
// Returns: {'name': 'Alice', 'age': 31}

applyUpdate(obj, '{"city": "NYC"}');
// Returns: {'name': 'Alice', 'age': 30, 'city': 'NYC'}

applyUpdate('not an object', '{"x": 1}');
// Returns: 'not an object' (unchanged)

Implementation

dynamic applyUpdate(dynamic value, String updates) {
  if (value is! Map) return value;

  try {
    final updateMap = json.jsonDecode(updates);
    return applyUpdateJson(value, updateMap);
  } catch (e) {
    _log.warning('Failed to apply update: $e');
    return value;
  }
}