THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.
This tutorial shows how to insert the data at beginning of a list with examples in dart
List inserts the elements in the insertion order. In the below program, add method inserts the elements to the end of a List.
void main() {
List wordsList = ['one', 'two', 'three'];
wordsList.add("five");
print(wordsList);
}
Output:
[one, two, three, five]
List
insert method provides inserting an element at the index position.
with this method, It increases the size of the list and the list must be growable.
void insert(int index, dynamic element)
void main() {
List wordsList = ['one', 'two', 'three'];
wordsList.insert(0,"five");
print(wordsList);
}
Output:
[five, one, two, three]
Similarly, you can add at the start of a list using another way using for-in loop syntax
void main() {
List wordsList = ['one', 'two', 'three'];
wordsList = [
"five",
for (String item in wordsList) item,
];
print(wordsList);
}
Output:
[five, one, two, three]
🧮 Tags
Recent posts
Multiple ways to iterate a loop with index and element in array in swift How to reload a page/component in Angular? How to populate enum object data in a dropdown in angular| Angular material dropdown example How to get the current date and time in local and UTC in Rust example Angular 13 UpperCase pipe tutorial | How to Convert a String to Uppercase exampleRelated posts