deepCopy<T> function

T? deepCopy<T>(
  1. T? o, {
  2. Copier? copier,
})

Deeply copies o.

  • List: copied with the same element type (a List<int> stays a List<int>); a typed-data list (such as Uint8List) keeps its class.
  • Map: copied into a new LinkedHashMap, keeping the key and value types of o when they can be reproduced (Dart can't create a map with the type arguments of another instance): keys of type String, int, Object or dynamic, and values of a primitive type (String, int, double, bool, num, nullable or not), a List or a Map<String, *> of those, Object or dynamic. A map with other key/value types is copied with a wider type (such as Map<Object, Object>) when that still satisfies T, otherwise the original instance is returned (shared, not copied, even with a copier). Nested in a collection, such a map is only replaced by its wider copy when the collection surely accepts it (a map, or a list of dynamic, Object or Map<String, dynamic>); otherwise the original instance is kept.
  • Other objects: copier is used, if provided, otherwise the same instance is returned.

copier Copy Function for non-primitive types.

Implementation

T? deepCopy<T>(T? o, {Copier? copier}) {
  if (o == null) return null;
  var copy = _deepCopyImpl(o, copier);
  return copy is T ? copy : o;
}