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".
We specify the Type Template using the
TypeTemplate
annotation. 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 holdingS
to 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
Iterable2Entity
instead ofIterableEntity
.$primeCollection
works the same way$collectionToIterable
returns a record where the first field is the first iterable (ofR1
), and the second field is the second iterable (ofR2
).$castCollection
casts the collection from holdingS1
andS2
to holdingR1
andR2
.(Optional) You can also override
$description1
and$description2
.
The methods $primeCollection
, $collectionToIterable
and $castCollection
can have as many type arguments as needed so that the parameters and the return types are fully typed. You can check out the source code of MappedValuesEntity
and MapEntity
as examples.
For both IterableEntity
and Iterable2Entity
kinds, you should never override $validateContent
.
Last updated