Dart .addAll()
Published May 13, 2024
Contribute to Docs
In Dart, the .addAll() method adds all collection elements to the end of a queue. This method is part of the Queue class in the dart:collection library. It is useful for merging two collections where the order of the elements is preserved and duplicates are not removed.
Syntax
queue.addAll(collection)
queue: The instance of aQueuewhere elements will be added.collection: The collection of elements to add to the queue. This can be of any class implementing theIterableinterface.
Note: The
.addAll()method does not return a value.
Example
In the following example, the .addAll() method takes all elements from the moreNumbers list and adds them to the end of the numbers queue:
import 'dart:collection';void main() {Queue<int> numbers = Queue.from([1, 2, 3]);List<int> moreNumbers = [4, 3, 5, 6];print(numbers);numbers.addAll(moreNumbers);print(numbers);}
The output for the above code is:
{1, 2, 3}{1, 2, 3, 4, 3, 5, 6}
Note: Since the
Queueclass is part of thedart:collectionlibrary, the library must be imported into the code to makeQueuework as intended.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Dart on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours