I can call list.add() which adds at the end but there is no convenient way to add an entry to a specific index which at the same time grows the list.
list.insert(index, elem) -- inserts the element at the given index, shifting elements to the right. list.extend(list2) adds the elements in list2 to the end of the list. Using + or += on a list is similar to using extend().
Use the insert() function(inserts the provided value at the specified position) to insert the given item at the specified index into the list by passing the index value and item to be inserted as arguments to it.
The insert() method inserts an element to the list at the specified index.
Dart 2
var idx = 3;
list.insert(idx, 'foo');
Depends on whether you want to insert a single item or a bunch of items
All available methods https://api.dartlang.org/stable/2.1.0/dart-core/List-class.html#instance-methods
You can use insertRange, it will grow the list when adding new elements.
var list = ["1","3","4"];
list.insertRange(0, 1, "0");
list.insertRange(2, 1, "2");
list.forEach((e) => print(e));
You can try it out on the DartBoard here
You can use the insert()
and removeAt()
methods now.
Ok, it seams as if list.insertRange(index, range, [elem]) is what i am looking for.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With