Skip to main content

Flutter Dart ListQueue Example

ListQueue is a type of List based on Queue format. ListQueue keeps cyclic buffer of items within and it can increase size when large mount of data fills in.

Flutter Dart ListQueue Example

1. Import dart:collection package in your flutter project main.dart file.
import 'dart:collection'; 
2. Creating a Integer type ListQueue.
final testLQ = ListQueue<int>();
3. Inserting items in ListQueue.
testLQ.add(2);
4. Adding item at first position in ListQueue  .
queue.addFirst(1);
5. Adding item at the last position in ListQueue .
testLQ.addLast(5);
6. Inserting multiple items in ListQueue.
testLQ.addAll([3, 4, 6]);
7. Check ListQueue is Empty or Not.
print(testLQ.isEmpty);
8. Check ListQueue length.
print(testLQ.length);
9. Get first item of ListQueue.
print(testLQ.first);
10. Get last item of ListQueue.
print(testLQ.last);
11. Remove First item from ListQueue.
testLQ.removeFirst()
12. Remove last item from ListQueue.
testLQ.removeLast();
13. Printing all items with For loop on screen one by one in ListQueue.
 for (var entry in testLQ) {
    print(entry);
  }
14. Deleting all items from ListQueue.
testLQ.clear();
15. Remove single item from ListQueue.
testLQ.remove(6);
Source code for main.dart file:
import 'dart:collection'; 

void main() { 
  final testLQ = ListQueue<int>();
  print(testLQ);
  testLQ.add(2);
  print(testLQ);
  testLQ.addFirst(1);
  print(testLQ);
  testLQ.addLast(5);
  print(testLQ);
  testLQ.addAll([3, 4, 6]);
  print(testLQ);
  print(testLQ.isEmpty);
  print(testLQ.length);
  print(testLQ.first);
  print(testLQ.last);
  testLQ.removeFirst();
  print(testLQ);
  testLQ.removeLast();
  print(testLQ);
  for (var entry in testLQ) {
    print(entry);
  }
  testLQ.remove(6);
  print(testLQ);
  testLQ.clear();
  print(testLQ);
}
Output:
Flutter Dart ListQueue Example

Comments