Adding elements: add and insert (all)

on Monday, 20th of July, 2020

Adding elements to lists is done with four elements. Importantly, this doesn't apply to the Iterable super class. These methods exist across different collections, such as List and Set, but not all methods exist on all collections.

  1. add(element) (list, set)
    • adds one element, passed in as an argument, to the end of a list.
  2. addAll([element1, element2, etc]) (list, set, map)
    • adds all the elements of one list to the end of the target list, maintaining ordering
  3. insert(index, element) (list)
    • adds one element to the list at the given index, and then moves every element over 1 to make room
  4. insertAll(index, [element1, element2, etc]) (list)
    • adds all elements passed in as a list to the target list beginning at the index provided, and shifting the rest of the elements over.

These methods exist only on certain collections for good reason, which I'll discuss after we look at some examples of using these methods with a list.

    
List cities = ['Portland', 'Medellin', 'Tokyo'];
void main() {
    print(cities);
    cities.add('Budapest');
    print(cities);
    cities.addAll(['Louisville', 'Bangkok']);
    print(cities);
    cities.insert(1, 'Chaing Mai');
    print(cities[1]);
    cities.insertAll(0, ['Paris', 'Baghdad', 'New York']);
    print(cities);
}

To determine if (or why) certain methods live on certain collections and not others, consider how these objects work. A Map has no use for a method like insert or insertAll, because it doesn't maintain or care about order. Similarly, not all Set implementations care about order.

These methods of course have an infinite number of use-cases. But one that's easy to show is using a list to represent a queue. A queue is an abstract data type that has one main rule: first-in-first-out. In human words, that means that the only element that can be removed from a queue is the element that was inserted first. Queue's can be nicely visualized like this:

queue drawing

Because of this, a queue needs to be able to insert elements at one end, and remove from the other end. This sort of fine control over the order of a list makes insert quite useful.

Try it yourself, make a queue



Join thousands of Flutter developers.

Sign up for infrequent updates about Flutter and Dart.

You can get all this content and more in one place. Check out my new book Flutter in Action