dart

Dart Program to add Single or Multiple Items to a List

You can use .add() method to add single item to a list and .addAll() method to add multiple items to a list in Dart programming language.

void main() {
  var myList = ["One", "Two"];
  
  //Add single item
  myList.add("Three");
  print(myList);
  
  //Add multiple items
  myList.addAll(["Three", "Four", "Five"]);
  print(myList);
}
Output
[One, Two, Three]
[One, Two, Three, Three, Four, Five]

.add() method

.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"]

.addAll()

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"]
Was this helpful?