NonGrowableListMixin<E> abstract class
Mixin class that implements a throwing version of all list operations that change the List's length.
abstract class NonGrowableListMixin<E> implements List<E> { static void _throw() { throw new UnsupportedError( "Cannot change the length of a fixed-length list"); } /** * Throws an [UnsupportedError]; * operations that change the length of the list are disallowed. */ void set length(int newLength) => _throw(); /** * Throws an [UnsupportedError]; * operations that change the length of the list are disallowed. */ bool add(E value) { _throw(); } /** * Throws an [UnsupportedError]; * operations that change the length of the list are disallowed. */ void addAll(Iterable<E> iterable) => _throw(); /** * Throws an [UnsupportedError]; * operations that change the length of the list are disallowed. */ void insert(int index, E element) => _throw(); /** * Throws an [UnsupportedError]; * operations that change the length of the list are disallowed. */ void insertAll(int index, Iterable<E> iterable) => _throw(); /** * Throws an [UnsupportedError]; * operations that change the length of the list are disallowed. */ bool remove(Object value) { _throw(); } /** * Throws an [UnsupportedError]; * operations that change the length of the list are disallowed. */ E removeAt(int index) { _throw(); } /** * Throws an [UnsupportedError]; * operations that change the length of the list are disallowed. */ E removeLast() { _throw(); } /** * Throws an [UnsupportedError]; * operations that change the length of the list are disallowed. */ void removeWhere(bool test(E element)) => _throw(); /** * Throws an [UnsupportedError]; * operations that change the length of the list are disallowed. */ void retainWhere(bool test(E element)) => _throw(); /** * Throws an [UnsupportedError]; * operations that change the length of the list are disallowed. */ void removeRange(int start, int end) => _throw(); /** * Throws an [UnsupportedError]; * operations that change the length of the list are disallowed. */ void replaceRange(int start, int end, Iterable<E> iterable) => _throw(); /** * Throws an [UnsupportedError]; * operations that change the length of the list are disallowed. */ void clear() => _throw(); }
Implements
Properties
final E first #
Returns the first element.
If this
is empty throws a StateError. Otherwise this method is
equivalent to this.elementAt(0)
E get first;
final bool isEmpty #
Returns true if there is no element in this collection.
bool get isEmpty;
final bool isNotEmpty #
Returns true if there is at least one element in this collection.
bool get isNotEmpty;
final Iterator<E> iterator #
Returns an Iterator that iterates over this Iterable object.
Iterator<E> get iterator;
abstract int get length #
Returns the number of objects in this list.
The valid indices for a list are 0
through length - 1
.
void set length(int newLength) #
Throws an UnsupportedError; operations that change the length of the list are disallowed.
void set length(int newLength) => _throw();
final Iterable<E> reversed #
Returns an Iterable of the objects in this list in reverse order.
Iterable<E> get reversed;
final E single #
Returns the single element in this
.
If this
is empty or has more than one element throws a StateError.
E get single;
Operators
abstract E operator [](int index) #
Returns the object at the given index in the list or throws a RangeError if index is out of bounds.
abstract 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.
Methods
bool add(E value) #
Throws an UnsupportedError; operations that change the length of the list are disallowed.
bool add(E value) { _throw(); }
void addAll(Iterable<E> iterable) #
Throws an UnsupportedError; operations that change the length of the list are disallowed.
void addAll(Iterable<E> iterable) => _throw();
abstract bool any(bool test(E element)) #
Returns true if one element of this collection satisfies the predicate test. Returns false otherwise.
abstract 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]
void clear() #
Throws an UnsupportedError; operations that change the length of the list are disallowed.
void clear() => _throw();
abstract bool contains(Object element) #
Returns true if the collection contains an element equal to element.
abstract E elementAt(int index) #
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
.
abstract bool every(bool test(E element)) #
Returns true if every elements of this collection satisify the
predicate
test. Returns false
otherwise.
abstract 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
.
abstract E firstWhere(bool test(E element), {E orElse()}) #
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.
abstract dynamic fold(initialValue, combine(previousValue, E element)) #
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);
abstract void forEach(void f(E element)) #
Applies the function f to each element of this collection.
abstract 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'
abstract 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
void insert(int index, E element) #
Throws an UnsupportedError; operations that change the length of the list are disallowed.
void insert(int index, E element) => _throw();
void insertAll(int index, Iterable<E> iterable) #
Throws an UnsupportedError; operations that change the length of the list are disallowed.
void insertAll(int index, Iterable<E> iterable) => _throw();
String join([String separator = ""]) #
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.
String join([String separator = ""]) { StringBuffer buffer = new StringBuffer(); buffer.writeAll(this, separator); return buffer.toString(); }
abstract 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
abstract E lastWhere(bool test(E element), {E orElse()}) #
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.
abstract Iterable map(f(E element)) #
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.
abstract E reduce(E combine(E value, E element)) #
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);
bool remove(Object value) #
Throws an UnsupportedError; operations that change the length of the list are disallowed.
bool remove(Object value) { _throw(); }
E removeAt(int index) #
Throws an UnsupportedError; operations that change the length of the list are disallowed.
E removeAt(int index) { _throw(); }
E removeLast() #
Throws an UnsupportedError; operations that change the length of the list are disallowed.
E removeLast() { _throw(); }
void removeRange(int start, int end) #
Throws an UnsupportedError; operations that change the length of the list are disallowed.
void removeRange(int start, int end) => _throw();
void removeWhere(bool test(E element)) #
Throws an UnsupportedError; operations that change the length of the list are disallowed.
void removeWhere(bool test(E element)) => _throw();
void replaceRange(int start, int end, Iterable<E> iterable) #
Throws an UnsupportedError; operations that change the length of the list are disallowed.
void replaceRange(int start, int end, Iterable<E> iterable) => _throw();
void retainWhere(bool test(E element)) #
Throws an UnsupportedError; operations that change the length of the list are disallowed.
void retainWhere(bool test(E element)) => _throw();
abstract 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.
abstract 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.
abstract void shuffle([Random random]) #
Shuffles the elements of this list randomly.
abstract E singleWhere(bool test(E element)) #
Returns the single element that satisfies test. If no or more than one element match then a StateError is thrown.
abstract Iterable<E> skip(int n) #
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.
abstract Iterable<E> skipWhile(bool test(E value)) #
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.
abstract 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'
abstract 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
.
abstract Iterable<E> takeWhile(bool test(E value)) #
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.
abstract Iterable<E> where(bool test(E element)) #
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.