Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

firstwhere
listqueue
dart
communitycreator

What is the ListQueue.firstWhere() method 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. Read more about ListQueue here.

The firstWhere method will loop through all the elements of the queue in the iteration order and check each element against the test condition. It will return the first element that satisfies the provided predicate function.

Syntax

E firstWhere(bool test(E el),{E orElse()?})

Parameter

This method takes two arguments

  1. test: This is a predicatePredicate is a functional interface which takes one argument and returns either true or false based on the condition defined. function which will be checked against the elements of the queue.
  2. orElse: This is a function that will be invoked when no element of the queue satisfies the passed test predicate function. The value returned from this orElse method is returned as the return value of the firstWhere method. Note that this is an optional argument.

Return value

This method returns the first element that satisfies the provided test condition. If no element satisfies the condition, the orElse function argument is invoked and the value returned from that function is returned.

Code

The code below demonstrates how to use the firstWhere method:

import 'dart:collection';
void main() {
// create a queue
ListQueue queue = new ListQueue();
// add 3 elements to the queue
queue.add(10);
queue.add(20);
queue.add(30);
print('The queue is : $queue');
// Get the first element which is greater than 10
var result = queue.firstWhere( (e)=> e > 10 );
print('The first element which is greater than 10 : $result');
// Get the first element which is less than 0
result = queue.firstWhere( (e)=> e < 0, orElse: ()=> null);
print('The first element which is less than 0: $result');
}
Using the firstWhere method to get the first element that satisfies the given predicate function

Explanation

In the code above:

  • In line 1, we import the collection library.

  • In line 4, we create a ListQueue with the name queue.

  • In lines 7-9, we use the add method to add three elements 10, 20, 30 to the queue. Now the queue is {10,20,30}.

  • In line 13, we use the firstWhere method with a predicate function. The predicate function will check if the element is greater than 10. In our case, the element 20 is the first element that is greater than 10. So, 20 is returned as the result.

  • In line 17, we use the firstWhere method with a predicate function and an orElse function. The predicate function will check if the element is negative. In our case, all elements of the queue are positive so the orElse method is invoked and null is returned from the orElse method.

RELATED TAGS

firstwhere
listqueue
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