asCollection method
Returns this type as a resolvable Iterable type.
This convenience method attempts to view this ResolvableType as an Iterable. If this type implements or extends Iterable, it returns the appropriate ResolvableType. Otherwise, it returns ResolvableType.NONE.
Returns:
- ResolvableType representing this type as an Iterable
- ResolvableType.NONE if this type is not iterable
Example:
final listType = ResolvableType.forClass(List<String>);
final setType = ResolvableType.forClass(Set<int>);
final stringType = ResolvableType.forClass(String);
final listAsCollection = listType.asCollection();
print(listAsCollection != ResolvableType.NONE); // true
final setAsCollection = setType.asCollection();
print(setAsCollection != ResolvableType.NONE); // true
final stringAsCollection = stringType.asCollection();
print(stringAsCollection == ResolvableType.NONE); // true
// Useful for generic collection processing
void processIfIterable(ResolvableType type) {
final asCollection = type.asCollection();
if (asCollection != ResolvableType.NONE) {
print("Can iterate over this type");
// Process as iterable
}
}
Implementation
ResolvableType asCollection() => as(Iterable);