Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

dart
communitycreator

What is ListQueue.last 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.

ListQueue is a list based on Queue. We can read more about ListQueue here.

The last property lets us get the last element present in the ListQueue.

Syntax

queue.last

Parameter

This property requires no parameter.

Return value

If the queue is empty, a StateError is thrown. Otherwise, it loops over the elements and returns the one viewed most recently.

Code

The code below demonstrates how to get the last element of the queue.

import 'dart:collection';
void main() {
// Create a new ListQueue object
ListQueue queue = new ListQueue();
// Add two elements to the queue
queue.add(10);
queue.add(20);
print("queue is : $queue");
// Get the last element of the queue
var lastEle = queue.last;
print("last element of the queue is : $lastEle");
// Remove all elements of the queue
queue.clear();
try {
print(queue.last);
} catch(e) {
print(e);
}
}
Using the `last` property

Explanation

  • Line 1: We import the collection library.

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

  • Lines 7 and 8: We use the add method to add two elements: 10 and 20.

  • Line 12: We get the last element of the queue using the last property, and store that value in a variable. In this case, we get 20 as the result.

  • Line 15: We use the clear method to delete all elements present in the queue. When we call this method, the queue becomes empty.

  • Line 17: We try to access the last property on the empty queue. This throws a StateError that states no element is present in the queue.

RELATED TAGS

dart
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