Dart Tutorials

Dart List remove()
Syntax & Examples

Syntax of List.remove()

The syntax of List.remove() method is:

 bool remove(Object? value) 

This remove() method of List removes the first occurrence of value from this list.

Parameters

ParameterOptional/RequiredDescription
valuerequiredthe value to remove from the list

Return Type

List.remove() returns value of type bool.



✐ Examples

1 Remove a number from the list

In this example,

  1. We create a list named numbers containing integers.
  2. We use the remove() method to remove the value 3 from numbers.
  3. The remove() method returns true if the value was found and removed, false otherwise.
  4. We print the removal status and the list after removal to standard output.

Dart Program

void main() {
  List<int> numbers = [1, 2, 3, 4, 5];
  bool removed = numbers.remove(3);
  print('Removed: $removed, List after removal: $numbers');
}

Output

Removed: true, List after removal: [1, 2, 4, 5]

2 Remove a character from the list

In this example,

  1. We create a list named characters containing characters.
  2. We use the remove() method to remove the value 'c' from characters.
  3. The remove() method returns true if the value was found and removed, false otherwise.
  4. We print the removal status and the list after removal to standard output.

Dart Program

void main() {
  List<String> characters = ['a', 'b', 'c', 'd'];
  bool removed = characters.remove('c');
  print('Removed: $removed, List after removal: $characters');
}

Output

Removed: true, List after removal: [a, b, d]

3 Remove a string from the list

In this example,

  1. We create a list named fruits containing strings.
  2. We use the remove() method to remove the value 'apple' from fruits.
  3. The remove() method returns true if the value was found and removed, false otherwise.
  4. We print the removal status and the list after removal to standard output.

Dart Program

void main() {
  List<String> fruits = ['apple', 'banana', 'cherry', 'apple'];
  bool removed = fruits.remove('apple');
  print('Removed: $removed, List after removal: $fruits');
}

Output

Removed: true, List after removal: [banana, cherry, apple]

Summary

In this Dart tutorial, we learned about remove() method of List: the syntax and few working examples with output and detailed explanation for each example.