Skip to content

Instantly share code, notes, and snippets.

@dariocast
Created February 2, 2022 10:28
Show Gist options
  • Save dariocast/ff394a5eb066b5998603871fe915c922 to your computer and use it in GitHub Desktop.
Save dariocast/ff394a5eb066b5998603871fe915c922 to your computer and use it in GitHub Desktop.
How to clone a list in Dart
void main() {
final list = [1,2,3,4];
// Choose one approach
// final list2 = list.toList();
// final list2 = [...list];
// final list2 = List.of(list);
final list2 = List.from(list);
print(list);
print(list2);
// Two lists are not coupled, not same referenced
list.removeLast();
print(list);
print(list2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment