In Dart, you can copy a list by creating a new list and adding all elements from the original list to the new list.
In this example,
originalList
with some elements.copiedList
and initialize it with the elements of originalList
.copiedList
contains all elements of originalList
.void main() {
List<int> originalList = [1, 2, 3, 4];
List<int> copiedList = List<int>.from(originalList);
print('Original List: $originalList');
print('Copied List: $copiedList');
}
Original List: [1, 2, 3, 4] Copied List: [1, 2, 3, 4]
In this example,
originalList
with some elements.copiedList
and copy elements from originalList
using the spread operator (...).copiedList
contains all elements of originalList
.void main() {
List<int> originalList = [1, 2, 3, 4];
List<int> copiedList = [...originalList];
print('Original List: $originalList');
print('Copied List: $copiedList');
}
Original List: [1, 2, 3, 4] Copied List: [1, 2, 3, 4]
In this tutorial, we learned How to Copy a List in Dart language with well detailed examples.