Created
February 2, 2022 10:28
-
-
Save dariocast/ff394a5eb066b5998603871fe915c922 to your computer and use it in GitHub Desktop.
How to clone a list in Dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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