What is the queue.first method in Dart?

The queue.first function in Dart is used to get the reference of the first element in the queue.

Figure 1 shows the visual representation of the queue.first function.

Figure 1: Visual representation of queue.first function

The following module is required in order to use the function dart:collection:

Syntax

element queue_name.first
// where the queue_name is the name of the queue

Parameter

This function does not require a parameter.

Return value

The function queue.first returns the reference to the queue’s first element.

  • It does not remove that element from the queue.
  • If the queue is empty then it throws an error.

Code

The following code shows how to use queue.first function in Dart.

import 'dart:convert';
import 'dart:collection';
void main() {
Queue<int> queue_1 = new Queue<int>();
queue_1.add(1);
queue_1.add(3);
queue_1.add(5);
queue_1.add(2);
queue_1.add(0);
//Queue filled= 1->3->5->2->0
print("The elements of queue_1: ${queue_1}");
print("The first element of queue_1: ${queue_1.first}");
}

Free Resources