Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

listqueue
first
dart
data structure
communitycreator

What is the ListQueue.first property in Dart?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

In Dart, ListQueue is a list based on Queue.

To get the first element in the ListQueue, we use the first property.

Note: You can read more about ListQueue here.

Synax

queue.first

Return value

If queue is empty, it throws a state error. Otherwise, it returns the first value of the ListQueue.

Code

The below code demonstrates how to get the first element of the queue:

import 'dart:collection';
void main() {
// create a queue
ListQueue queue = new ListQueue();
// add two elements
queue.add(10);
queue.add(20);
print("queue is : $queue");
// access the first element of the queue
var firstEle = queue.first;
print("First element of the queue is : $firstEle");
// clear elements of the queue
queue.clear();
try {
print(queue.first);
} catch(e) {
print(e);
}
}
Using the first property

Explanation

  • Line 1: We import the collection library.

  • Line 4: We create a ListQueue with the name queue.

  • Lines 6 and 7: We Use the add method to add two elements, 10 and 20.

  • Line 11: We use the first property to get the first element, and store that value in a variable. In our case, we’ll get 10 as result.

  • Line 14: We used the clear method to delete all elements present in the queue. After calling this method, the queue becomes empty.

  • Line 16: We access the first property on the empty queue. This throws a state error, and returns no element present in the queue,

RELATED TAGS

listqueue
first
dart
data structure
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring