.clear()

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

In Dart, the .clear() method removes all elements in the queue. After invoking this method, the queue will be empty, and its size will be zero.

The .clear() method is a (void) method that does not return any value and does not accept any arguments.

Syntax

To call the .clear() method and clear a queue named myQueue, the syntax is as follows:

myQueue.clear();

Note: To use a queue in a Dart program, it is necessary to import the dart:collection module.

Example

In the following example, a queue is created, elements are inserted in the queue and the .clear() method removes all elements in the queue:

import 'dart:collection';
void main()
{
// Creating a queue
Queue<int> myQueue = new Queue<int>();
// Inserting elements in the myQueue
myQueue.addAll([1, 2, 3, 4, 5, 6]);
print("Queue before clearing: $myQueue");
// removing all elements in the myQueue
myQueue.clear();
print("Queue after clearing: $myQueue");
}

The above code will result in the following output:

Queue before clearing: {1, 2, 3, 4, 5, 6}
Queue after clearing: {}

All contributors

Contribute to Docs

Learn Dart on Codecademy