Skip to content

Instantly share code, notes, and snippets.

@alperenarc
Created December 8, 2020 08:17
Show Gist options
  • Save alperenarc/427814879e78e2191b34a2557c7fb6e5 to your computer and use it in GitHub Desktop.
Save alperenarc/427814879e78e2191b34a2557c7fb6e5 to your computer and use it in GitHub Desktop.
Reference and Value type in Dart
void main() {
List<A> baseList = [];
baseList.add(A(value: 1, description: 'Alperen Arıcı'));
baseList.add(A(value: 2, description: 'İsmail Özalp'));
baseList.add(A(value: 3, description: 'Teyyihan Aksu'));
baseList.add(A(value: 4, description: 'Alperen Ünal'));
List<A> secondList = [];
for (final model in baseList) {
A cloningModel = A.clone(model);
secondList.add(cloningModel);
}
secondList[0].description = 'Changing Description';
secondList[0].value = -10;
printList(baseList);
}
printList(List<A> modelList) {
modelList.forEach(
(element) => print(element.value.toString() + ' ' + element.description));
}
class A {
int value;
String description;
A({this.value, this.description});
A.clone(A source) {
this.value = source.value;
this.description = source.description;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment