deepcopy 1.1.2 copy "deepcopy: ^1.1.2" to clipboard
deepcopy: ^1.1.2 copied to clipboard

simple recursive deep copy for nested collections (List, Set, Map)

example/lib/example.dart

import 'package:deepcopy/deepcopy.dart';

void main(List<String> arguments) {
  // Test list cloning
  List originalList = [
    1,
    2,
    [
      3,
      4,
      [
        5,
        6,
        [7, 8]
      ]
    ],
    {'name': 'John', 'age': 30}
  ];
  // clone it!
  List clonedList = originalList.deepcopy();

  // some changes
  clonedList[0] = 100;
  clonedList[2][2][2][0] = 700;

  print(
      originalList); // Output: [1, 2, [3, 4, [5, 6, [7, 8]]], {name: John, age: 30}]
  print(
      clonedList); // Output: [100, 2, [3, 4, [5, 6, [700, 8]]], {name: John, age: 30}]

  // Test map cloning
  Map originalMap = {
    'person': {'name': 'John', 'age': 30},
    'numbers': [1, 2, 3],
    'nested': {
      'a': {
        'b': {
          'c': {'d': 'deep'}
        }
      }
    }
  };

  // clone it!
  Map clonedMap = originalMap.deepcopy();

  // some changes
  originalMap['person']['name'] = 'Jane';
  originalMap['nested']['a']['b']['c']['d'] = 'modified';
  print(
      originalMap); // Output: {person: {name: Jane, age: 30}, numbers: [1, 2, 3], nested: {a: {b: {c: {d: modified}}}}}
  print(
      clonedMap); // Output: {person: {name: John, age: 30}, numbers: [1, 2, 3], nested: {a: {b: {c: {d: deep}}}}}

  // Test set cloning
  Set originalSet = {
    'apple',
    'banana',
    {'carrot'},
    ['durian']
  };
  Set clonedSet = originalSet.deepcopy();

  originalSet.clear();

  print(originalSet); // Output: {}
  print(clonedSet); // Output: {apple, banana, {carrot}, [durian]}
}
9
likes
160
points
3.23k
downloads
screenshot

Publisher

verified publisherletyarch.blogspot.com

Weekly Downloads

simple recursive deep copy for nested collections (List, Set, Map)

Repository (GitHub)
View/report issues

Topics

#deepcopy #collection

Documentation

API reference

License

BSD-2-Clause (license)

More

Packages that depend on deepcopy