A collection of values, or "elements", that can be accessed sequentially.
The elements of the iterable are accessed by getting anIteratorusing theiterator getter, and using it to step through the values.Stepping with the iterator is done by callingIterator.moveNext,and if the call returnstrue,the iterator has now moved to the next element,which is then available asIterator.current.If the call returnsfalse, there are no more elements.TheIterator.current value must only be used when the mostrecent call toIterator.moveNext has returnedtrue.If it is used before callingIterator.moveNext the first timeon an iterator, or after a call has returned false or has thrown an error,readingIterator.current may throw or may return an arbitrary value.
You can create more than one iterator from the sameIterable.Each timeiterator is read, it returns a new iterator,and different iterators can be stepped through independently,each giving access to all the elements of the iterable.The iterators of the same iterableshould provide the same valuesin the same order (unless the underlying collection is modified betweenthe iterations, which some collections allow).
You can also iterate over the elements of anIterableusing the for-in loop construct, which uses theiterator getter behind thescenes.For example, you can iterate over all of the keys of aMap,becauseMap keys are iterable.
var kidsBooks = {'Matilda': 'Roald Dahl', 'Green Eggs and Ham': 'Dr Seuss', 'Where the Wild Things Are': 'Maurice Sendak'};for (var book in kidsBooks.keys) { print('$book was written by ${kidsBooks[book]}');}TheList andSet classes are bothIterable,as are most classes in thedart:collection library.
SomeIterable collections can be modified.Adding an element to aList orSet will change which elements itcontains, and adding a new key to aMap changes the elements ofMap.keys.Iterators created after the change will provide the new elements, and mayor may not preserve the order of existing elements(for example, aHashSet may completely change its order when a singleelement is added).
Changing a collectionwhile it is being iteratedis generallynot allowed.Doing so will break the iteration, which is typically signalledby throwing aConcurrentModificationErrorthe next timeIterator.moveNext is called.The current value ofIterator.current gettershould not be affected by the change in the collection,thecurrent value was set by the previous call toIterator.moveNext.
Some iterables compute their elements dynamically every time they areiterated, like the one returned byIterable.generate or the iterablereturned by async* generator function. If the computation doesn't dependon other objects that may change, then the generated sequence should bethe same one every time it's iterated.
The members ofIterable, other thaniterator itself,work by looking at the elements of the iterable.This can be implemented by running through theiterator, but some classesmay have more efficient ways of finding the result(likelast orlength on aList, orcontains on aSet).
The methods that return anotherIterable (likemap andwhere)are alllazy - they will iterate the original (as necessary)every time the returned iterable is iterated, and not before.
Since an iterable may be iterated more than once, it's not recommended tohave detectable side-effects in the iterator.For methods likemap andwhere, the returned iterable will execute theargument function on every iteration, so those functions should also nothave side effects.
TheIterable declaration provides a default implementation,which can be extended or mixed in to implement theIterable interface.It implements every member other than theiterator getter,using theIterator provided byiterator.An implementation of theIterable interface shouldprovide a more efficient implementation of the members ofIterablewhen it can do so.
Constructors
- Iterable()
- const
- Iterable.empty()
- Creates an empty iterable.constfactory
- Iterable.generate(intcount, [Egenerator(intindex)?])
- Creates an
Iterablewhich generates its elements dynamically.factory - Iterable.withIterator(Iterator<
E> iteratorFactory()) - Creates anIterable from theIterator factory.factory
Properties
- first→ E
- The first element.no setter
- firstOrNull→ T?
Available onIterable<
The first element of this iterator, orT> , provided by theIterableExtensions extensionnullif the iterable is empty.no setter- hashCode→int
- The hash code for this object.no setterinherited
- indexed→Iterable<
(int,T)> Available onIterable<
Pairs of elements of the indices and elements of this iterable.T> , provided by theIterableExtensions extensionno setter- isEmpty→bool
- Whether this collection has no elements.no setter
- isNotEmpty→bool
- Whether this collection has at least one element.no setter
- iterator→Iterator<
E> - A new
Iteratorthat allows iterating the elements of thisIterable.no setter - last→ E
- The last element.no setter
- lastOrNull→ T?
Available onIterable<
The last element of this iterable, orT> , provided by theIterableExtensions extensionnullif the iterable is empty.no setter- length→int
- The number of elements in thisIterable.no setter
- nonNulls→Iterable<
T> Available onIterable<
The non-T?> , provided by theNullableIterableExtensions extensionnullelements of this iterable.no setter- runtimeType→Type
- A representation of the runtime type of the object.no setterinherited
- single→ E
- Checks that this iterable has only one element, and returns that element.no setter
- singleOrNull→ T?
Available onIterable<
The single element of this iterator, orT> , provided by theIterableExtensions extensionnull.no setter- wait→Future<
List< T> > Available onIterable<
Waits for futures in parallel.Future< , provided by theFutureIterable extensionT> >no setter
Methods
- any(
booltest(Eelement))→bool - Checks whether any element of this iterable satisfies
test. - asNameMap(
)→Map< String,T> Available onIterable<
Creates a map from the names of enum values to the values.T> , provided by theEnumByName extension- byName(
Stringname)→ T Available onIterable<
Finds the enum value in this list with nameT> , provided by theEnumByName extensionname.- cast<
R> ()→Iterable< R> - A view of this iterable as an iterable of
Rinstances. - contains(
Object?element)→bool - Whether the collection contains an element equal to
element. - elementAt(
intindex)→ E - Returns the
indexth element. - elementAtOrNull(
intindex)→ T? Available onIterable<
The element at positionT> , provided by theIterableExtensions extensionindexof this iterable, ornull.- every(
booltest(Eelement))→bool - Checks whether every element of this iterable satisfies
test. - expand<
T> (Iterable< T> toElements(Eelement))→Iterable<T> - Expands each element of thisIterable into zero or more elements.
- firstWhere(
booltest(Eelement), {EorElse()?})→ E - The first element that satisfies the given predicate
test. - fold<
T> (TinitialValue,Tcombine(TpreviousValue,Eelement))→ T - Reduces a collection to a single value by iteratively combining eachelement of the collection with an existing value
- followedBy(
Iterable< E> other)→Iterable<E> - Creates the lazy concatenation of this iterable and
other. - forEach(
voidaction(Eelement))→ void - Invokes
actionon each element of this iterable in iteration order. - join(
[Stringseparator =""])→String - Converts each element to aString and concatenates the strings.
- lastWhere(
booltest(Eelement), {EorElse()?})→ E - The last element that satisfies the given predicate
test. - map<
T> (TtoElement(Ee))→Iterable< T> - The current elements of this iterable modified by
toElement. - noSuchMethod(
Invocationinvocation)→ dynamic - Invoked when a nonexistent method or property is accessed.inherited
- reduce(
Ecombine(Evalue,Eelement))→ E - Reduces a collection to a single value by iteratively combining elementsof the collection using the provided function.
- singleWhere(
booltest(Eelement), {EorElse()?})→ E - The single element that satisfies
test. - skip(
intcount)→Iterable< E> - Creates anIterable that provides all but the first
countelements. - skipWhile(
booltest(Evalue))→Iterable< E> - Creates an
Iterablethat skips leading elements whiletestis satisfied. - take(
intcount)→Iterable< E> - Creates a lazy iterable of the
countfirst elements of this iterable. - takeWhile(
booltest(Evalue))→Iterable< E> - Creates a lazy iterable of the leading elements satisfying
test. - toList(
{boolgrowable =true})→List< E> - Creates aList containing the elements of thisIterable.
- toSet(
)→Set< E> - Creates aSet containing the same elements as this iterable.
- toString(
)→String - Returns a string representation of (some of) the elements of
this.override - where(
booltest(Eelement))→Iterable< E> - Creates a new lazyIterable with all elements that satisfy thepredicate
test. - whereType<
T> ()→Iterable< T> - Creates a new lazyIterable with all elements that have type
T.
Operators
- operator ==(
Objectother)→bool - The equality operator.inherited
Static Methods
- castFrom<
S,T> (Iterable< S> source)→Iterable<T> - Adapts
sourceto be anIterable<T>. - iterableToFullString(
Iterableiterable, [StringleftDelimiter ='(',StringrightDelimiter =')'])→String - Converts an
Iterableto a string. - iterableToShortString(
Iterableiterable, [StringleftDelimiter ='(',StringrightDelimiter =')'])→String - Convert an
Iterableto a string likeIterable.toString.