Closed justinbot closed 8 months ago
I will look into it
@justinbot Ok so:
Reason is that you (a) use a custom mapper and (b) that encodes its content in kind of a "transparent" way, meaning in the json there is no trace of the GenericBox class. So when the decode
method of the custom mapper is called T
will always be the static type of the element (here Animal) and never the dynamic type, because that gets only decoded later when the content itself is decoded (by using the 'type' parameter in the animal jsons).
To also get the "real" type statically for GenericBox you can use my type_plus
package that is also used internally by dart_mappable like this:
@override
GenericBox decode<T>(dynamic value) {
T content = MapperContainer.globals.fromValue<T>(value);
// This uses the `provideTo` extension from `package:type_plus` to provide the runtimeType as a static type parameter.
return content.runtimeType.provideTo(<RuntimeT>() {
return GenericBox<RuntimeT>(content as RuntimeT);
});
}
Hi, I may just be missing something here -- but it seems when using a generic custom mapper for a generic container class, the container loses the subtype of its generic parameter.
Consider this example where we have a mappable class with some subtypes:
And a generic container class with custom mapper:
And a mappable class which includes generic containers:
Now when a
Storage
instance is encoded and decoded, theGenericBox.content
values have their subtypes as expected (Cat
,Dog
). However the issue is that eachGenericBox
itself is only aGenericBox<Animal>
, rather thanGenericBox<Cat>
andGenericBox<Dog>
.Is it possible for the generic containers to be decoded in a way that includes their parameter subtypes?