void main() {
var myList = ["One", "Two"];
//Add single item
myList.add("Three");
print(myList);
//Add multiple items
myList.addAll(["Three", "Four", "Five"]);
print(myList);
}
.add(newItem)
You can use .add() method to add single item to your list collection in dart. Always remember that the type of item that you are inserting to dart list must be same of list items type.
For Example if there is a list of employee names and you want to add a new name to this list then you can use below code.
void main() {
var employeeNames = <String>["John", "Rick"];
employeeNames.add("Carol");
print(employeeNames);
}
Output
["John", "Rick", "Carol"]
To add multiple items to a list you can use .addAll() method of dart. The basic syntax of .addAll() method is as below.
.addAll(<VarType>[])
For example if you want to add multiple items to the former list of employees you can use below code
void main() {
var employeeNames = <String>["John", "Rick"];
employeeNames.addAll(["Carol", "Maven", "Shasha"]);
print(employeeNames);
}
Output
["John", "Rick", "Carol", "Maven", "Shasha"]
0 Comments