Creating your own IterableEntity / Iterable2Entity kind
You can easily create your own IterableEntity / Iterable2Entity kind that holds any collection of your choice, as long as it can be converted to one/two iterables.
For example, let's create an IterableEntity which holds a two-dimensional list of modddels (A list of lists of modddels). Let's name it "MultiListEntity".
// 1.
@TypeTemplate('List<List<#1>>')
// 2
abstract class MultiListEntity<I extends InvalidEntity, V extends ValidEntity>
    extends IterableEntity<I, V> {
  // 3
  @protected
  @optionalTypeArgs
  List<List<R>> $primeCollection<R>(List<List<R>> collection) {
    final nestedLists = collection.map(List.unmodifiable);
    return List.unmodifiable(nestedLists);
  }
  // 4
  @protected
  @optionalTypeArgs
  Iterable<R> $collectionToIterable<R>(List<List<R>> collection) {
    return collection.expand((nestedList) => nestedList);
  }
  // 5
  @protected
  @optionalTypeArgs
  List<List<R>> $castCollection<S, R>(List<List<S>> source) {
    return source.map((nestedList) => nestedList.cast<R>()).toList();
  }
  // 6
  @override
  String $description(int index) => 'MultiList item $index';
}We specify the Type Template using the
TypeTemplateannotation. Because we're making an IterableEntity, it must contain one mask only : "#1".We declare MultiListEntity as an abstract class that extends
IterableEntity.We declare a method named
$primeCollection: This is a method that modifies the collection each time an instance of the Modddel is created. It's mainly used when you have a dart collection that you want to make unmodifiable. Otherwise, you can just directly return the collection.We declare a method named
$collectionToIterable: This method converts the collection to an iterable ofR.We declare a method named
$castCollection: This method casts the collection from holdingSto holdingR(fromList<List<S>>toList<List<R>>).(Optional) You can also override
$description.
The process of making an Iterable2Entity is pretty similar, with a few differences :
The TypeTemplate must contain two masks : "#1" and "#2".
You should extend
Iterable2Entityinstead ofIterableEntity.$primeCollectionworks the same way$collectionToIterablereturns a tuple (Tuple2) where the first value is the first iterable (ofR1), and the second value is the second iterable (ofR2).$castCollectioncasts the collection from holdingS1andS2to holdingR1andR2.(Optional) You can also override
$description1and$description2.
For both IterableEntity and Iterable2Entity kinds, you should never override $validateContent.
Last updated