deepClone function
Returns a copy of a data structure if it consists of only DynamicMaps, DynamicLists, ints, doubles, bools, and Strings.
This is relatively expensive as the entire data structure must be walked and new objects created.
Implementation
Object? deepClone(Object? template) {
if (template == null) {
return null;
} else if (template is DynamicMap) {
return template.map((String key, Object? value) => MapEntry<String, Object?>(key, deepClone(value)));
} else if (template is DynamicList) {
return template.map((Object? value) => deepClone(value)).toList();
} else {
assert(template is int || template is double || template is bool || template is String, 'unexpected state object type: ${template.runtimeType} ($template)');
return template;
}
}