TouchList class
TouchList represents a list of all of the points of contact with a touch surface; for example, if the user has three fingers on the screen (or trackpad), the corresponding TouchList would have one Touch
object for each finger, for a total of three entries.
@DomName('TouchList')
class TouchList extends Interceptor with ListMixin<Touch>, ImmutableListMixin<Touch> implements JavaScriptIndexingBehavior, List<Touch> native "TouchList" {
/// NB: This constructor likely does not work as you might expect it to! This
/// constructor will simply fail (returning null) if you are not on a device
/// with touch enabled. See dartbug.com/8314.
factory TouchList() => document.$dom_createTouchList();
/// Checks if this type is supported on the current platform.
static bool get supported => JS('bool', '!!document.createTouchList');
@DomName('TouchList.length')
@DocsEditable
int get length => JS("int", "#.length", this);
Touch operator[](int index) {
if (JS("bool", "# >>> 0 !== # || # >= #", index,
index, index, length))
throw new RangeError.range(index, 0, length);
return JS("Touch", "#[#]", this, index);
}
void operator[]=(int index, Touch value) {
throw new UnsupportedError("Cannot assign element of immutable List.");
}
// -- start List<Touch> mixins.
// Touch is the element type.
void set length(int value) {
throw new UnsupportedError("Cannot resize immutable List.");
}
Touch get first {
if (this.length > 0) {
return JS('Touch', '#[0]', this);
}
throw new StateError("No elements");
}
Touch get last {
int len = this.length;
if (len > 0) {
return JS('Touch', '#[#]', this, len - 1);
}
throw new StateError("No elements");
}
Touch get single {
int len = this.length;
if (len == 1) {
return JS('Touch', '#[0]', this);
}
if (len == 0) throw new StateError("No elements");
throw new StateError("More than one element");
}
Touch elementAt(int index) => this[index];
// -- end List<Touch> mixins.
@DomName('TouchList.item')
@DocsEditable
Touch item(int index) native;
}
Extends
Interceptor > Interceptor_ListMixin > Interceptor_ListMixin_ImmutableListMixin > TouchList
Implements
List<Touch>, JavaScriptIndexingBehavior
Static Properties
Constructors
factory TouchList() #
NB: This constructor likely does not work as you might expect it to! This constructor will simply fail (returning null) if you are not on a device with touch enabled. See dartbug.com/8314.
factory TouchList() => document.$dom_createTouchList();
Properties
final int hashCode #
Get a hash code for this object.
All objects have hash codes. Hash codes are guaranteed to be the
same for objects that are equal when compared using the equality
operator ==. Other than that there are no guarantees about
the hash codes. They will not be consistent between runs and
there are no distribution guarantees.
If a subclass overrides hashCode it should override the
equality operator as well to maintain consistency.
int get hashCode => Primitives.objectHashCode(this);
final bool isEmpty #
final Iterator<E> iterator #
Iterator<E> get iterator {
// Note: NodeLists are not fixed size. And most probably length shouldn't
// be cached in both iterator _and_ forEach method. For now caching it
// for consistency.
return new FixedSizeListIterator<E>(this);
}
final Iterable<E> reversed #
final Type runtimeType #
A representation of the runtime type of the object.
Type get runtimeType => getRuntimeType(this);
final Touch single #
Returns the single element in this.
If this is empty or has more than one element throws a StateError.
Touch get single {
int len = this.length;
if (len == 1) {
return JS('Touch', '#[0]', this);
}
if (len == 0) throw new StateError("No elements");
throw new StateError("More than one element");
}
Operators
Touch operator [](int index) #
Returns the element at the given
index in the list or throws
an RangeError if
index is out of bounds.
Touch operator[](int index) {
if (JS("bool", "# >>> 0 !== # || # >= #", index,
index, index, length))
throw new RangeError.range(index, 0, length);
return JS("Touch", "#[#]", this, index);
}
bool operator ==(other) #
The equality operator.
The default behavior for all Objects is to return true if and
only if this and
other are the same object.
If a subclass overrides the equality operator it should override
the hashCode method as well to maintain consistency.
bool operator ==(other) => identical(this, other);
Methods
void add(E value) #
void addAll(Iterable<E> iterable) #
Appends all elements of the
iterable to the end of this list.
Extends the length of the list by the number of elements in
iterable.
Throws an UnsupportedError if this list is not extensible.
void addAll(Iterable<E> iterable) {
throw new UnsupportedError("Cannot add to immutable List.");
}
bool any(bool test(E element)) #
Returns true if one element of this collection satisfies the
predicate f. Returns false otherwise.
bool any(bool test(E element)) {
int length = this.length;
for (int i = 0; i < length; i++) {
if (test(this[i])) return true;
if (length != this.length) {
throw new ConcurrentModificationError(this);
}
}
return false;
}
Map<int, E> asMap() #
void clear() #
bool contains(E element) #
bool contains(E element) {
int length = this.length;
for (int i = 0; i < length; i++) {
if (this[i] == element) return true;
if (length != this.length) {
throw new ConcurrentModificationError(this);
}
}
return false;
}
Touch elementAt(int index) #
Returns the indexth element.
If [this] [Iterable] 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.
Touch elementAt(int index) => this[index];
bool every(bool test(E element)) #
Returns true if every elements of this collection satisify the
predicate f. Returns false otherwise.
bool every(bool test(E element)) {
int length = this.length;
for (int i = 0; i < length; i++) {
if (!test(this[i])) return false;
if (length != this.length) {
throw new ConcurrentModificationError(this);
}
}
return true;
}
Iterable expand(Iterable f(E element)) #
Expand each element of this Iterable into zero or more elements.
The resulting Iterable will run through the elements returned by f for each element of this, in order.
The returned Iterable is lazy, and will call
f for each element
of this every time it's iterated.
Iterable expand(Iterable f(E element)) => new ExpandIterable<E, dynamic>(this, f);
void fillRange(int start, int end, [E fillValue]) #
E firstWhere(bool test(E element), {E orElse()}) #
Returns the first element that satisfies the given predicate f.
If none matches, the result of invoking the
orElse function is
returned. By default, when
orElse is null, a StateError is
thrown.
E firstWhere(bool test(E element), { E orElse() }) {
int length = this.length;
for (int i = 0; i < length; i++) {
E element = this[i];
if (test(element)) return element;
if (length != this.length) {
throw new ConcurrentModificationError(this);
}
}
if (orElse != null) return orElse();
throw new StateError("No matching element");
}
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);
fold(var initialValue, combine(var previousValue, E element)) {
var value = initialValue;
int length = this.length;
for (int i = 0; i < length; i++) {
value = combine(value, this[i]);
if (length != this.length) {
throw new ConcurrentModificationError(this);
}
}
return value;
}
void forEach(void action(E element)) #
Iterable<E> getRange(int start, int end) #
Returns an Iterable that iterators over the elements in the range
start to
end exclusive. The result of this function
is backed by this.
It is an error if end is before start.
It is an error if the
start and
end are not valid ranges at the time
of the call to this method. The returned Iterable behaves similar to
skip(start).take(end - start). That is, it will not throw exceptions
if this changes size.
Example:
var list = [1, 2, 3, 4, 5];
var range = list.getRange(1, 4);
print(range.join(', ')); // => 2, 3, 4
list.length = 3;
print(range.join(', ')); // => 2, 3
Iterable<E> getRange(int start, int end) {
_rangeCheck(start, end);
return new SubListIterable(this, start, end);
}
int indexOf(E element, [int startIndex = 0]) #
Returns the first index of element in the list.
Searches the list from index start to the length of the list.
The first time an element e is encountered so that e == element,
the index of e is returned.
Returns -1 if
element is not found.
int indexOf(E element, [int startIndex = 0]) {
if (startIndex >= this.length) {
return -1;
}
if (startIndex < 0) {
startIndex = 0;
}
for (int i = startIndex; i < this.length; i++) {
if (this[i] == element) {
return i;
}
}
return -1;
}
void insert(int index, E element) #
Inserts the element at position index in the list.
This increases the length of the list by one and shifts all elements at or after the index towards the end of the list.
It is an error if the index does not point inside the list or at the position after the last element.
void insert(int index, E element) {
throw new UnsupportedError("Cannot add to immutable List.");
}
void insertAll(int index, Iterable<E> iterable) #
Inserts all elements of iterable at position index in the list.
This increases the length of the list by the length of iterable and shifts all later elements towards the end of the list.
It is an error if the index does not point inside the list or at the position after the last element.
void insertAll(int index, Iterable<E> iterable) {
throw new UnsupportedError("Cannot add to immutable List.");
}
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 = ""]) {
int length = this.length;
if (!separator.isEmpty) {
if (length == 0) return "";
String first = "${this[0]}";
if (length != this.length) {
throw new ConcurrentModificationError(this);
}
StringBuffer buffer = new StringBuffer(first);
for (int i = 1; i < length; i++) {
buffer.write(separator);
buffer.write(this[i]);
if (length != this.length) {
throw new ConcurrentModificationError(this);
}
}
return buffer.toString();
} else {
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < length; i++) {
buffer.write(this[i]);
if (length != this.length) {
throw new ConcurrentModificationError(this);
}
}
return buffer.toString();
}
}
int lastIndexOf(E element, [int startIndex]) #
Returns the last index in the list a of the given
element, starting
the search at index
startIndex to 0.
Returns -1 if
element is not found.
int lastIndexOf(E element, [int startIndex]) {
if (startIndex == null) {
startIndex = this.length - 1;
} else {
if (startIndex < 0) {
return -1;
}
if (startIndex >= this.length) {
startIndex = this.length - 1;
}
}
for (int i = startIndex; i >= 0; i--) {
if (this[i] == element) {
return i;
}
}
return -1;
}
E lastWhere(bool test(E element), {E orElse()}) #
Returns the last element that satisfies the given predicate f.
If none matches, the result of invoking the
orElse function is
returned. By default, when
orElse is null, a StateError is
thrown.
E lastWhere(bool test(E element), { E orElse() }) {
int length = this.length;
for (int i = length - 1; i >= 0; i--) {
E element = this[i];
if (test(element)) return element;
if (length != this.length) {
throw new ConcurrentModificationError(this);
}
}
if (orElse != null) return orElse();
throw new StateError("No matching element");
}
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.
Iterable map(f(E element)) => new MappedListIterable(this, f);
dynamic noSuchMethod(Invocation invocation) #
noSuchMethod is invoked when users invoke a non-existant method
on an object. The name of the method and the arguments of the
invocation are passed to noSuchMethod in an Invocation.
If noSuchMethod returns a value, that value becomes the result of
the original invocation.
The default behavior of noSuchMethod is to throw a
noSuchMethodError.
dynamic noSuchMethod(Invocation invocation) {
throw new NoSuchMethodError(
this,
_symbolToString(invocation.memberName),
invocation.positionalArguments,
_symbolMapToStringMap(invocation.namedArguments));
}
E reduce(E combine(E previousValue, 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);
E reduce(E combine(E previousValue, E element)) {
if (length == 0) throw new StateError("No elements");
E value = this[0];
for (int i = 1; i < length; i++) {
value = combine(value, this[i]);
}
return value;
}
void remove(Object object) #
E removeAt(int pos) #
Removes the element at position index from the list.
This reduces the length of the list by one and moves all later elements
down by one position.
Returns the removed element.
Throws an ArgumentError if index is not an int.
Throws an RangeError if the index does not point inside
the list.
Throws an UnsupportedError, and doesn't remove the element,
if the length of the list cannot be changed.
E removeAt(int pos) {
throw new UnsupportedError("Cannot remove from immutable List.");
}
E removeLast() #
void removeRange(int start, int end) #
void removeWhere(bool test(E element)) #
void replaceRange(int start, int end, Iterable<E> iterable) #
Removes the elements in the range start to end exclusive and replaces them with the contents of the iterable.
It is an error if
start..
end is not a valid range pointing into the
this.
Example:
var list = [1, 2, 3, 4, 5];
list.replaceRange(1, 3, [6, 7, 8, 9]);
print(list); // [1, 6, 7, 8, 9, 4, 5]
void replaceRange(int start, int end, Iterable<E> iterable) {
throw new UnsupportedError("Cannot modify an immutable List.");
}
void retainWhere(bool test(E element)) #
void setAll(int index, Iterable<E> iterable) #
Overwrites elements of this with the elemenst of
iterable starting
at position
index in the list.
This operation does not increase the length of the list.
It is an error if the index does not point inside the list or at the position after the last element.
It is an error if the iterable is longer than length - index.
void setAll(int index, Iterable<E> iterable) {
throw new UnsupportedError("Cannot modify an immutable List.");
}
void setRange(int start, int end, Iterable<E> iterable, [int skipCount]) #
Copies the elements of
iterable, skipping the
skipCount first elements
into the range
start to
end exclusive of this.
If start equals end and represent a legal range, this method has no effect.
It is an error if
start..
end is not a valid range pointing into the
this.
It is an error if the iterable does not have enough elements after skipping skipCount elements.
void setRange(int start, int end, Iterable<E> iterable, [int skipCount]) {
throw new UnsupportedError("Cannot setRange on immutable List.");
}
E singleWhere(bool test(E element)) #
Returns the single element that satisfies f. If no or more than one
element match then a StateError is thrown.
E singleWhere(bool test(E element)) {
int length = this.length;
E match = null;
bool matchFound = false;
for (int i = 0; i < length; i++) {
E element = this[i];
if (test(element)) {
if (matchFound) {
throw new StateError("More than one matching element");
}
matchFound = true;
match = element;
}
if (length != this.length) {
throw new ConcurrentModificationError(this);
}
}
if (matchFound) return match;
throw new StateError("No matching element");
}
Iterable<E> skip(int count) #
Iterable<E> skipWhile(bool test(E element)) #
Returns an Iterable that skips elements while
test is satisfied.
The filtering happens lazily. Every new Iterator of the returned
Iterable will iterate over all elements of this.
As long as the iterator's elements do not satisfy
test they are
discarded. Once an element satisfies the
test the iterator stops testing
and uses every element unconditionally.
Iterable<E> skipWhile(bool test(E element)) {
return new SkipWhileIterable<E>(this, test);
}
void sort([int compare(E a, E b)]) #
Sorts the list according to the order specified by the compare function.
The
compare function must act as a Comparator.
The default List implementations use Comparable.compare if
compare is omitted.
void sort([int compare(E a, E b)]) {
throw new UnsupportedError("Cannot sort immutable List.");
}
List<E> sublist(int start, [int end]) #
Returns a new list containing the elements from start to end.
If end is omitted, the length of the list is used.
It is an error if start or end are not list indices for this list, or if end is before start.
List<E> sublist(int start, [int end]) {
if (end == null) end = length;
_rangeCheck(start, end);
int length = end - start;
List<E> result = new List<E>()..length = length;
for (int i = 0; i < length; i++) {
result[i] = this[start + i];
}
return result;
}
Iterable<E> take(int count) #
Iterable<E> takeWhile(bool test(E element)) #
Returns an Iterable that stops once
test is not satisfied anymore.
The filtering happens lazily. Every new Iterator of the returned
Iterable will start 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 will not
ask or provide any more elements.
Iterable<E> takeWhile(bool test(E element)) {
return new TakeWhileIterable<E>(this, test);
}
List<E> toList({bool growable: true}) #
Creates a List containing the elements of this Iterable.
The elements will be in iteration order. The list is fixed-length if growable is false.
List<E> toList({ bool growable: true }) {
List<E> result;
if (growable) {
result = new List<E>()..length = length;
} else {
result = new List<E>(length);
}
for (int i = 0; i < length; i++) {
result[i] = this[i];
}
return result;
}
Set<E> toSet() #
String toString() #
Returns a string representation of this object.
String toString() => ToString.iterableToString(this);
Iterable<E> where(bool test(E element)) #
Returns a lazy Iterable with all elements that satisfy the
predicate f.
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. Iterating will not cache results, and thus iterating
multiple times over the the returned Iterable will invoke the supplied
function f multiple times on the same element.
Iterable<E> where(bool test(E element)) => new WhereIterable<E>(this, test);
This page includes content from the
Mozilla Foundation that is graciously
licensed under a
Creative Commons: Attribution-Sharealike license.
Mozilla has no other association with Dart or dartlang.org. We
encourage you to improve the web by
contributing to
The Mozilla Developer Network.