.add()

Anonymous contributor's avatar
Anonymous contributor
Published Jul 9, 2024
Contribute to Docs

The .add() method in Dart is used to append a single element to the end of a list. It modifies the original list and extends its length by one to accommodate the new element.

Syntax

listName.add(element);
  • element: The element to be appended to the list.

Example

In the following example, the .add() method appends the string ‘orange’ to the end of the fruits list.

void main() {
List<String> fruits = ['apple', 'banana'];
fruits.add('orange');
print(fruits);
}

The output of the above code will be:

[apple, banana, orange]

All contributors

Contribute to Docs

Learn Dart on Codecademy