To append an element to a list in Dart, you can use the add
method for a single element or the addAll
method for multiple elements.
In this example,
numbers
with initial integer values.add
method of the list numbers
to append a new integer to the list.add
method modifies the original list by adding the new element at the end.void main() {
List<int> numbers = [1, 2, 3, 4];
numbers.add(5);
print('Updated list: \$numbers');
}
Updated list: [1, 2, 3, 4, 5]
In this example,
numbers
with initial integer values.newElements
containing elements to be appended.addAll
method to add all elements from newElements
to numbers
at once.void main() {
List<int> numbers = [1, 2, 3, 4];
List<int> newElements = [5, 6, 7];
numbers.addAll(newElements);
print('Updated list: \$numbers');
}
Updated list: [1, 2, 3, 4, 5, 6, 7]
In this tutorial, we learned How to Append an Element to a List in Dart language with well detailed examples.