Movatterモバイル変換


[0]ホーム

URL:


menu
  1. Dart
  2. dart:core
  3. Iterable<E> class
Iterable
description

Iterable<E> classabstractmixin

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.

Implementers
Available extensions

Constructors

Iterable()
const
Iterable.empty()
Creates an empty iterable.
const
factory
Iterable.generate(intcount, [Egenerator(intindex)?])
Creates anIterable which 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<T>, provided by theIterableExtensions extension

The first element of this iterator, ornull if the iterable is empty.
no setter
hashCodeint
The hash code for this object.
no setterinherited
indexedIterable<(int,T)>

Available onIterable<T>, provided by theIterableExtensions extension

Pairs of elements of the indices and elements of this iterable.
no setter
isEmptybool
Whether this collection has no elements.
no setter
isNotEmptybool
Whether this collection has at least one element.
no setter
iteratorIterator<E>
A newIterator that allows iterating the elements of thisIterable.
no setter
last→ E
The last element.
no setter
lastOrNull→ T?

Available onIterable<T>, provided by theIterableExtensions extension

The last element of this iterable, ornull if the iterable is empty.
no setter
lengthint
The number of elements in thisIterable.
no setter
nonNullsIterable<T>

Available onIterable<T?>, provided by theNullableIterableExtensions extension

The non-null elements of this iterable.
no setter
runtimeTypeType
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<T>, provided by theIterableExtensions extension

The single element of this iterator, ornull.
no setter
waitFuture<List<T>>

Available onIterable<Future<T>>, provided by theFutureIterable extension

Waits for futures in parallel.
no setter

Methods

any(booltest(Eelement))bool
Checks whether any element of this iterable satisfiestest.
asNameMap()Map<String,T>

Available onIterable<T>, provided by theEnumByName extension

Creates a map from the names of enum values to the values.
byName(Stringname)→ T

Available onIterable<T>, provided by theEnumByName extension

Finds the enum value in this list with namename.
cast<R>()Iterable<R>
A view of this iterable as an iterable ofR instances.
contains(Object?element)bool
Whether the collection contains an element equal toelement.
elementAt(intindex)→ E
Returns theindexth element.
elementAtOrNull(intindex)→ T?

Available onIterable<T>, provided by theIterableExtensions extension

The element at positionindex of this iterable, ornull.
every(booltest(Eelement))bool
Checks whether every element of this iterable satisfiestest.
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 predicatetest.
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 andother.
forEach(voidaction(Eelement))→ void
Invokesaction on 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 predicatetest.
map<T>(TtoElement(Ee))Iterable<T>
The current elements of this iterable modified bytoElement.
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 satisfiestest.
skip(intcount)Iterable<E>
Creates anIterable that provides all but the firstcount elements.
skipWhile(booltest(Evalue))Iterable<E>
Creates anIterable that skips leading elements whiletest is satisfied.
take(intcount)Iterable<E>
Creates a lazy iterable of thecount first elements of this iterable.
takeWhile(booltest(Evalue))Iterable<E>
Creates a lazy iterable of the leading elements satisfyingtest.
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 ofthis.
override
where(booltest(Eelement))Iterable<E>
Creates a new lazyIterable with all elements that satisfy thepredicatetest.
whereType<T>()Iterable<T>
Creates a new lazyIterable with all elements that have typeT.

Operators

operator ==(Objectother)bool
The equality operator.
inherited

Static Methods

castFrom<S,T>(Iterable<S>source)Iterable<T>
Adaptssource to be anIterable<T>.
iterableToFullString(Iterableiterable, [StringleftDelimiter ='(',StringrightDelimiter =')'])String
Converts anIterable to a string.
iterableToShortString(Iterableiterable, [StringleftDelimiter ='(',StringrightDelimiter =')'])String
Convert anIterable to a string likeIterable.toString.
  1. Dart
  2. dart:core
  3. Iterable<E> class
dart:core library

[8]ページ先頭

©2009-2025 Movatter.jp