deepCopy<T> function
Deeply copies o.
- List: copied with the same element type (a
List<int>stays aList<int>); a typed-data list (such asUint8List) keeps its class. - Map: copied into a new LinkedHashMap, keeping the key and value
types of
owhen they can be reproduced (Dart can't create a map with the type arguments of another instance): keys of typeString,int,Objectordynamic, and values of a primitive type (String,int,double,bool,num, nullable or not), aListor aMap<String, *>of those,Objectordynamic. A map with other key/value types is copied with a wider type (such asMap<Object, Object>) when that still satisfiesT, otherwise the original instance is returned (shared, not copied, even with acopier). 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 ofdynamic,ObjectorMap<String, dynamic>); otherwise the original instance is kept. - Other objects:
copieris 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;
}