Dart Documentationdart.pkg.collection.wrappersDelegatingList<E>

DelegatingList<E> class

Creates a List that delegates all operations to a base list.

This class can be used hide non-List methods of a list object, or it can be extended to add extra functionality on top of an existing list object.

class DelegatingList<E> extends DelegatingIterable<E> implements List<E> {
 DelegatingList(List<E> base) : super(base);

 List<E> get _listBase => _base;

 E operator [](int index) => _listBase[index];

 void operator []=(int index, E value) {
   _listBase[index] = value;
 }

 void add(E value) {
   _listBase.add(value);
 }

 void addAll(Iterable<E> iterable) {
   _listBase.addAll(iterable);
 }

 Map<int, E> asMap() => _listBase.asMap();

 void clear() {
   _listBase.clear();
 }

 void fillRange(int start, int end, [E fillValue]) {
   _listBase.fillRange(start, end, fillValue);
 }

 Iterable<E> getRange(int start, int end) => _listBase.getRange(start, end);

 int indexOf(E element, [int start = 0]) => _listBase.indexOf(element, start);

 void insert(int index, E element) {
   _listBase.insert(index, element);
 }

 void insertAll(int index, Iterable<E> iterable) {
   _listBase.insertAll(index, iterable);
 }

 int lastIndexOf(E element, [int start]) =>
     _listBase.lastIndexOf(element, start);

 void set length(int newLength) {
   _listBase.length = newLength;
 }

 bool remove(Object value) => _listBase.remove(value);

 E removeAt(int index) => _listBase.removeAt(index);

 E removeLast() => _listBase.removeLast();

 void removeRange(int start, int end) {
   _listBase.removeRange(start, end);
 }

 void removeWhere(bool test(E element)) {
   _listBase.removeWhere(test);
 }

 void replaceRange(int start, int end, Iterable<E> iterable) {
   _listBase.replaceRange(start, end, iterable);
 }

 void retainWhere(bool test(E element)) {
   _listBase.retainWhere(test);
 }

 Iterable<E> get reversed => _listBase.reversed;

 void setAll(int index, Iterable<E> iterable) {
   _listBase.setAll(index, iterable);
 }

 void setRange(int start, int end, Iterable<E> iterable, [int skipCount = 0]) {
   _listBase.setRange(start, end, iterable, skipCount);
 }

 void shuffle([Random random]) {
   _listBase.shuffle(random);
 }

 void sort([int compare(E a, E b)]) {
   _listBase.sort(compare);
 }

 List<E> sublist(int start, [int end]) => _listBase.sublist(start, end);
}

Extends

DelegatingIterable<E> > DelegatingList<E>

Implements

List<E>

Constructors

new DelegatingList(List<E> base) #

Create a wrapper that forwards operations to base.

docs inherited from DelegatingIterable<E>
DelegatingList(List<E> base) : super(base);

Properties

final E first #

inherited from DelegatingIterable

Returns the first element.

If this is empty throws a StateError. Otherwise this method is equivalent to this.elementAt(0)

docs inherited from Iterable<E>
E get first => _base.first;

final bool isEmpty #

inherited from DelegatingIterable

Returns true if there is no element in this collection.

docs inherited from Iterable<E>
bool get isEmpty => _base.isEmpty;

final bool isNotEmpty #

inherited from DelegatingIterable

Returns true if there is at least one element in this collection.

docs inherited from Iterable<E>
bool get isNotEmpty => _base.isNotEmpty;

final Iterator<E> iterator #

inherited from DelegatingIterable

Returns an Iterator that iterates over this Iterable object.

docs inherited from Iterable<E>
Iterator<E> get iterator => _base.iterator;

final E last #

inherited from DelegatingIterable

Returns the last element.

If this is empty throws a StateError.

docs inherited from Iterable<E>
E get last => _base.last;

int get length #

inherited from DelegatingIterable

Returns the number of elements in this.

Counting all elements may be involve running through all elements and can therefore be slow.

docs inherited from Iterable<E>
int get length => _base.length;

void set length(int newLength) #

Changes the length of this list.

If newLength is greater than the current length, entries are initialized to null.

Throws an UnsupportedError if the list is fixed-length.

docs inherited from List<E>
void set length(int newLength) {
 _listBase.length = newLength;
}

final Iterable<E> reversed #

Returns an Iterable of the objects in this list in reverse order.

docs inherited from List<E>
Iterable<E> get reversed => _listBase.reversed;

final E single #

inherited from DelegatingIterable

Returns the single element in this.

If this is empty or has more than one element throws a StateError.

docs inherited from Iterable<E>
E get single => _base.single;

Operators

E operator [](int index) #

Returns the object at the given index in the list or throws a RangeError if index is out of bounds.

docs inherited from List<E>
E operator [](int index) => _listBase[index];

void operator []=(int index, E value) #

Sets the value at the given index in the list to value or throws a RangeError if index is out of bounds.

docs inherited from List<E>
void operator []=(int index, E value) {
 _listBase[index] = value;
}

Methods

void add(E value) #

Adds value to the end of this list, extending the length by one.

Throws an UnsupportedError if the list is fixed-length.

docs inherited from List<E>
void add(E value) {
 _listBase.add(value);
}

void addAll(Iterable<E> iterable) #

Appends all objects of iterable to the end of this list.

Extends the length of the list by the number of objects in iterable. Throws an UnsupportedError if this list is fixed-length.

docs inherited from List<E>
void addAll(Iterable<E> iterable) {
 _listBase.addAll(iterable);
}

bool any(bool test(E element)) #

inherited from DelegatingIterable

Returns true if one element of this collection satisfies the predicate test. Returns false otherwise.

docs inherited from Iterable<E>
bool any(bool test(E element)) => _base.any(test);

Map<int, E> asMap() #

Returns an unmodifiable Map view of this.

The map uses the indices of this list as keys and the corresponding objects as values. The Map.keys Iterable iterates the indices of this list in numerical order.

List<String> words = ['fee', 'fi', 'fo', 'fum'];
Map<int, String> map = words.asMap();
map[0] + map[1];   // 'feefi';
map.keys.toList(); // [0, 1, 2, 3]
docs inherited from List<E>
Map<int, E> asMap() => _listBase.asMap();

void clear() #

Removes all objects from this list; the length of the list becomes zero.

Throws an UnsupportedError, and retains all objects, if this is a fixed-length list.

docs inherited from List<E>
void clear() {
 _listBase.clear();
}

bool contains(Object element) #

inherited from DelegatingIterable

Returns true if the collection contains an element equal to element.

docs inherited from Iterable<E>
bool contains(Object element) => _base.contains(element);

E elementAt(int index) #

inherited from DelegatingIterable

Returns the indexth element.

If this has fewer than index elements throws a RangeError.

Note: if this does not have a deterministic iteration order then the function may simply return any element without any iteration if there are at least index elements in this.

docs inherited from Iterable<E>
E elementAt(int index) => _base.elementAt(index);

bool every(bool test(E element)) #

inherited from DelegatingIterable

Returns true if every elements of this collection satisify the predicate test. Returns false otherwise.

docs inherited from Iterable<E>
bool every(bool test(E element)) => _base.every(test);

Iterable expand(Iterable f(E element)) #

inherited from DelegatingIterable

Expands each element of this Iterable into zero or more elements.

The resulting Iterable runs through the elements returned by f for each element of this, in order.

The returned Iterable is lazy, and calls f for each element of this every time it's iterated.

docs inherited from Iterable<E>
Iterable expand(Iterable f(E element)) => _base.expand(f);

void fillRange(int start, int end, [E fillValue]) #

Sets the objects in the range start inclusive to end exclusive to the given fillValue.

An error occurs if start.. end is not a valid range for this.

docs inherited from List<E>
void fillRange(int start, int end, [E fillValue]) {
 _listBase.fillRange(start, end, fillValue);
}

E firstWhere(bool test(E element), {E orElse()}) #

inherited from DelegatingIterable

Returns the first element that satisfies the given predicate test.

If none matches, the result of invoking the orElse function is returned. By default, when orElse is null, a StateError is thrown.

docs inherited from Iterable<E>
E firstWhere(bool test(E element), {E orElse()}) =>
   _base.firstWhere(test, orElse: orElse);

dynamic fold(initialValue, combine(previousValue, E element)) #

inherited from DelegatingIterable

Reduces a collection to a single value by iteratively combining each element of the collection with an existing value using the provided function.

Use initialValue as the initial value, and the function combine to create a new value from the previous one and an element.

Example of calculating the sum of an iterable:

iterable.fold(0, (prev, element) => prev + element);
docs inherited from Iterable<E>
fold(initialValue, combine(previousValue, E element)) =>
   _base.fold(initialValue, combine);

void forEach(void f(E element)) #

inherited from DelegatingIterable

Applies the function f to each element of this collection.

docs inherited from Iterable<E>
void forEach(void f(E element)) => _base.forEach(f);

Iterable<E> getRange(int start, int end) #

Returns an Iterable that iterates over the objects in the range start inclusive to end exclusive.

An error occurs if end is before start.

An error occurs if the start and end are not valid ranges at the time of the call to this method. The returned Iterable behaves like skip(start).take(end - start). That is, it does not throw exceptions if this changes size.

List<String> colors = ['red', 'green', 'blue', 'orange', 'pink'];
Iterable<String> range = colors.getRange(1, 4);
range.join(', ');  // 'green, blue, orange'
colors.length = 3;
range.join(', ');  // 'green, blue'
docs inherited from List<E>
Iterable<E> getRange(int start, int end) => _listBase.getRange(start, end);

int indexOf(E element, [int start = 0]) #

Returns the first index of element in this list.

Searches the list from index start to the end of the list. The first time an object o is encountered so that o == element, the index of o is returned.

List<String> notes = ['do', 're', 'mi', 're'];
notes.indexOf('re');    // 1
notes.indexOf('re', 2); // 3

Returns -1 if element is not found.

notes.indexOf('fa');    // -1
docs inherited from List<E>
int indexOf(E element, [int start = 0]) => _listBase.indexOf(element, start);

void insert(int index, E element) #

Inserts the object at position index in this list.

This increases the length of the list by one and shifts all objects at or after the index towards the end of the list.

An error occurs if the index is less than 0 or greater than length. An UnsupportedError occurs if the list is fixed-length.

docs inherited from List<E>
void insert(int index, E element) {
 _listBase.insert(index, element);
}

void insertAll(int index, Iterable<E> iterable) #

Inserts all objects of iterable at position index in this list.

This increases the length of the list by the length of iterable and shifts all later objects towards the end of the list.

An error occurs if the index is less than 0 or greater than length. An UnsupportedError occurs if the list is fixed-length.

docs inherited from List<E>
void insertAll(int index, Iterable<E> iterable) {
 _listBase.insertAll(index, iterable);
}

String join([String separator = ""]) #

inherited from DelegatingIterable

Converts each element to a String and concatenates the strings.

Converts each element to a String by calling Object.toString on it. Then concatenates the strings, optionally separated by the separator string.

docs inherited from Iterable<E>
String join([String separator = ""]) => _base.join(separator);

int lastIndexOf(E element, [int start]) #

Returns the last index of element in this list.

Searches the list backwards from index start to 0.

The first time an object o is encountered so that o == element, the index of o is returned.

List<String> notes = ['do', 're', 'mi', 're'];
notes.lastIndexOf('re', 2); // 1

If start is not provided, this method searches from the end of the list./Returns

notes.lastIndexOf('re');  // 3

Returns -1 if element is not found.

notes.lastIndexOf('fa');  // -1
docs inherited from List<E>
int lastIndexOf(E element, [int start]) =>
   _listBase.lastIndexOf(element, start);

E lastWhere(bool test(E element), {E orElse()}) #

inherited from DelegatingIterable

Returns the last element that satisfies the given predicate test.

If none matches, the result of invoking the orElse function is returned. By default, when orElse is null, a StateError is thrown.

docs inherited from Iterable<E>
E lastWhere(bool test(E element), {E orElse()}) =>
   _base.lastWhere(test, orElse: orElse);

Iterable map(f(E element)) #

inherited from DelegatingIterable

Returns a lazy Iterable where each element e of this is replaced by the result of f(e).

This method returns a view of the mapped elements. As long as the returned Iterable is not iterated over, the supplied function f will not be invoked. The transformed elements will not be cached. Iterating multiple times over the the returned Iterable will invoke the supplied function f multiple times on the same element.

docs inherited from Iterable<E>
Iterable map(f(E element)) => _base.map(f);

E reduce(E combine(E value, E element)) #

inherited from DelegatingIterable

Reduces a collection to a single value by iteratively combining elements of the collection using the provided function.

Example of calculating the sum of an iterable:

iterable.reduce((value, element) => value + element);
docs inherited from Iterable<E>
E reduce(E combine(E value, E element)) => _base.reduce(combine);

bool remove(Object value) #

Removes the first occurence of value from this list.

Returns true if value was in the list, false otherwise.

List<String> parts = ['head', 'shoulders', 'knees', 'toes'];
parts.remove('head'); // true
parts.join(', ');     // 'shoulders, knees, toes'

The method has no effect if value was not in the list.

// Note: 'head' has already been removed.
parts.remove('head'); // false
parts.join(', ');     // 'shoulders, knees, toes'

An UnsupportedError occurs if the list is fixed-length.

docs inherited from List<E>
bool remove(Object value) => _listBase.remove(value);

E removeAt(int index) #

Removes the object at position index from this list.

This method reduces the length of this by one and moves all later objects down by one position.

Returns the removed object.

The index must be in the range 0 ≤ index < length.

Throws an UnsupportedError if this is a fixed-length list. In that case the list is not modified.

docs inherited from List<E>
E removeAt(int index) => _listBase.removeAt(index);

E removeLast() #

Pops and returns the last object in this list.

Throws an UnsupportedError if this is a fixed-length list.

docs inherited from List<E>
E removeLast() => _listBase.removeLast();

void removeRange(int start, int end) #

Removes the objects in the range start inclusive to end exclusive.

The start and end indices must be in the range 0 ≤ index ≤ length, and start ≤ end.

Throws an UnsupportedError if this is a fixed-length list. In that case the list is not modified.

docs inherited from List<E>
void removeRange(int start, int end) {
 _listBase.removeRange(start, end);
}

void removeWhere(bool test(E element)) #

Removes all objects from this list that satisfy test.

An object o satisfies test if test(o) is true.

List<String> numbers = ['one', 'two', 'three', 'four'];
numbers.removeWhere((item) => item.length == 3);
numbers.join(', '); // 'three, four'

Throws an UnsupportedError if this is a fixed-length list.

docs inherited from List<E>
void removeWhere(bool test(E element)) {
 _listBase.removeWhere(test);
}

void replaceRange(int start, int end, Iterable<E> iterable) #

Removes the objects in the range start inclusive to end exclusive and inserts the contents of replacement in its place.

List<int> list = [1, 2, 3, 4, 5];
list.replaceRange(1, 4, [6, 7]);
list.join(', '); // '1, 6, 7, 5'

An error occurs if start.. end is not a valid range for this.

docs inherited from List<E>
void replaceRange(int start, int end, Iterable<E> iterable) {
 _listBase.replaceRange(start, end, iterable);
}

void retainWhere(bool test(E element)) #

Removes all objects from this list that fail to satisfy test.

An object o satisfies test if test(o) is true.

List<String> numbers = ['one', 'two', 'three', 'four'];
numbers.retainWhere((item) => item.length == 3);
numbers.join(', '); // 'one, two'

Throws an UnsupportedError if this is a fixed-length list.

docs inherited from List<E>
void retainWhere(bool test(E element)) {
 _listBase.retainWhere(test);
}

void setAll(int index, Iterable<E> iterable) #

Overwrites objects of this with the objects of iterable, starting at position index in this list.

List<String> list = ['a', 'b', 'c'];
list.setAll(1, ['bee', 'sea']);
list.join(', '); // 'a, bee, sea'

This operation does not increase the length of this.

The index must be non-negative and no greater than length.

The iterable must not have more elements than what can fit from index to length.

If iterable is based on this list, its values may change /during/ the setAll operation.

docs inherited from List<E>
void setAll(int index, Iterable<E> iterable) {
 _listBase.setAll(index, iterable);
}

void setRange(int start, int end, Iterable<E> iterable, [int skipCount = 0]) #

Copies the objects of iterable, skipping skipCount objects first, into the range start, inclusive, to end, exclusive, of the list.

List<int> list1 = [1, 2, 3, 4];
List<int> list2 = [5, 6, 7, 8, 9];
// Copies the 4th and 5th items in list2 as the 2nd and 3rd items
// of list1.
list1.setRange(1, 3, list2, 3);
list1.join(', '); // '1, 8, 9, 4'

The start and end indices must satisfy 0 ≤ start ≤ end ≤ length. If start equals end, this method has no effect.

The iterable must have enough objects to fill the range from start to end after skipping skipCount objects.

If iterable is this list, the operation will copy the elements originally in the range from skipCount to skipCount + (end - start) to the range start to end, even if the two ranges overlap.

If iterable depends on this list in some other way, no guarantees are made.

docs inherited from List<E>
void setRange(int start, int end, Iterable<E> iterable, [int skipCount = 0]) {
 _listBase.setRange(start, end, iterable, skipCount);
}

void shuffle([Random random]) #

Shuffles the elements of this list randomly.

docs inherited from List<E>
void shuffle([Random random]) {
 _listBase.shuffle(random);
}

E singleWhere(bool test(E element)) #

inherited from DelegatingIterable

Returns the single element that satisfies test. If no or more than one element match then a StateError is thrown.

docs inherited from Iterable<E>
E singleWhere(bool test(E element)) => _base.singleWhere(test);

Iterable<E> skip(int n) #

inherited from DelegatingIterable

Returns an Iterable that skips the first n elements.

If this has fewer than n elements, then the resulting Iterable is empty.

It is an error if n is negative.

docs inherited from Iterable<E>
Iterable<E> skip(int n) => _base.skip(n);

Iterable<E> skipWhile(bool test(E value)) #

inherited from DelegatingIterable

Returns an Iterable that skips elements while test is satisfied.

The filtering happens lazily. Every new Iterator of the returned Iterable iterates over all elements of this.

As long as the iterator's elements satisfy test they are discarded. Once an element does not satisfy the test the iterator stops testing and uses every later element unconditionally. That is, the elements of the returned Iterable are the elements of this starting from the first element that does not satisfy test.

docs inherited from Iterable<E>
Iterable<E> skipWhile(bool test(E value)) => _base.skipWhile(test);

void sort([int compare(E a, E b)]) #

Sorts this list according to the order specified by the compare function.

The compare function must act as a Comparator.

List<String> numbers = ['one', 'two', 'three', 'four'];
// Sort from shortest to longest.
numbers.sort((x, y) => x.length.compareTo(y.length));
numbers.join(', '); // 'one, two, four, three'

The default List implementations use Comparable.compare if compare is omitted.

List<int> nums = [13, 2, -11];
nums.sort();
     nums.join(', '); // '-11, 2, 13'
docs inherited from List<E>
void sort([int compare(E a, E b)]) {
 _listBase.sort(compare);
}

List<E> sublist(int start, [int end]) #

Returns a new list containing the objects from start inclusive to end exclusive.

List<String> colors = ['red', 'green', 'blue', 'orange', 'pink'];
colors.sublist(1, 3); // ['green', 'blue']

If end is omitted, the length of this is used.

colors.sublist(1);  // ['green', 'blue', 'orange', 'pink']

An error occurs if start is outside the range 0 .. length or if end is outside the range start .. length.

docs inherited from List<E>
List<E> sublist(int start, [int end]) => _listBase.sublist(start, end);

Iterable<E> take(int n) #

inherited from DelegatingIterable

Returns an Iterable with at most n elements.

The returned Iterable may contain fewer than n elements, if this contains fewer than n elements.

It is an error if n is negative.

docs inherited from Iterable<E>
Iterable<E> take(int n) => _base.take(n);

Iterable<E> takeWhile(bool test(E value)) #

inherited from DelegatingIterable

Returns an Iterable that stops once test is not satisfied anymore.

The filtering happens lazily. Every new Iterator of the returned Iterable starts iterating over the elements of this.

When the iterator encounters an element e that does not satisfy test, it discards e and moves into the finished state. That is, it does not get or provide any more elements.

docs inherited from Iterable<E>
Iterable<E> takeWhile(bool test(E value)) => _base.takeWhile(test);

List<E> toList({bool growable: true}) #

inherited from DelegatingIterable

Creates a List containing the elements of this Iterable.

The elements are in iteration order. The list is fixed-length if growable is false.

docs inherited from Iterable<E>
List<E> toList({bool growable: true}) => _base.toList(growable: growable);

Set<E> toSet() #

inherited from DelegatingIterable

Creates a Set containing the elements of this Iterable.

docs inherited from Iterable<E>
Set<E> toSet() => _base.toSet();

Iterable<E> where(bool test(E element)) #

inherited from DelegatingIterable

Returns a lazy Iterable with all elements that satisfy the predicate test.

This method returns a view of the mapped elements. As long as the returned Iterable is not iterated over, the supplied function test will not be invoked. Iterating will not cache results, and thus iterating multiple times over the returned Iterable will invoke the supplied function test multiple times on the same element.

docs inherited from Iterable<E>
Iterable<E> where(bool test(E element)) => _base.where(test);