-
Notifications
You must be signed in to change notification settings - Fork 2
/
example.dart
70 lines (60 loc) · 1.5 KB
/
example.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import 'package:deepcopy/deepcopy.dart';
abstract class Data {}
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]}
}