VOOZH about

URL: https://www.geeksforgeeks.org/dart/queues-in-dart/

⇱ Queues in Dart - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Queues in Dart

Last Updated : 25 Mar, 2025

Dart also provides the user to manipulate a collection of data in the form of a queue. A queue is a FIFO (First In First Out) data structure where the element that is added first will be deleted first. It takes the data from one end and removes it from the other end. Queues are useful when you want to build a first-in, first-out collection of data. It is a special case of list implementation of data in Dart.

Creating a Queue in Dart

-

Queue variable_name = new Queue();

-

// With type notation(E)
Queue<E> variable_name = new Queue<E>.from(list_name);

// Without type notation
var variable_name = new Queue.from(list_name);

It must be noted that to use a queue in a dart program you have to import 'dart:collection' module. If you don't do so then you will see the following Error:

Error compiling to JavaScript:
main.dart:6:3:
Error: 'Queue; isn't a type
Queue<String> geek = new Queue<String>();
^^^^^
main.dart:6:28:
Error: Method not found: 'Queue'.
Queue<String> geek = new Queue<String>();
^^^^^
Error: Compilation failed.


Example 1: Creating a queue through a constructor and then inserting the elements in it


Output:

{}
{Geeks, For, Geeks}

In the above code queue_name.add(element) is used to add the data in the queue.


Example 2: Creating a queue through a list


Output:

{Geeks, For, Geeks}


Functions of Queue in Dart

Dart also provides functions to manipulate queue created in the dart. Some important functions are listed in the table below followed by the example to use them.

Function Syntax

Description of the Function

queue_name.add(element)Adds the element inside the queue from the back.
queue_name.addAll(collection_name)

Adds all the element present in the collection_name (generally List).

queue_name.addFirst(element)Adds the element from front inside the queue.
queue_name.addLast(element)Adds the element from back in the queue.
queue_name.clear()Removes all the elements from the queue.
queue_name.first()Returns the first element from the queue.
queue_name.forEach(f(element))Returns all the element present in the queue.
queue_name.isEmptyReturns boolean true if the queue is empty else return false.
queue_name.lengthReturns the length of the queue.
queue_name.removeFirst()Removes the first element from the queue.
queue_name.removeLast()Removes the last element from the queue.


Example: Using various functions on Queue in Dart


Output:

{}
{Geeks}
{Geeks, For, Geeks}
{}
true
{Geeks}
{Geeks, For, Geeks}
3
{For, Geeks}
{For}
For


Conclusion

In Dart, queues offer an effective way to manage data collections based on the First In, First Out (FIFO) principle. They are especially useful for handling ordered data that needs to be processed sequentially.

Queues can be created using constructors or initialized from existing lists. To avoid compilation errors, it is essential to import the `dart:collection` module. Dart also provides various functions for manipulating queues, such as adding, removing, and retrieving elements, which enhances the flexibility of data handling.

By understanding queue operations in Dart, developers can create optimized, structured, and efficient applications that require sequential data processing.

Comment
Article Tags:
Article Tags:

Explore