You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is an odd one I've come across recently using the generic classes.
A solution seems to be to adding all possible subclasses to TypePlus using TypePlus.add().
My software uses this for unit conversions and uses a type parameter for Weight, Length, Angle (lots of these) etc to ensure that you can't subtract incorrect units,
My classses
classUnit {}
classLengthextendsUnit {}
classMassextendsUnit {}
...
classUnitValue<TextendsUnit> {
finalT unit;
finaldouble value;
// Supports various operations on values with units and handles conversions etc.
}
classUnitValueMapperextendsSimpleMapper1Bounded<UnitValue, Unit> {
constUnitValueMapper();
staticconst _valueKey ="val";
staticconst _unitKey ="unit";
staticfinal _unitRegistry = [
Length.meter,
Length.mile,
Length.yard,
...etc
];
// Get the unit from the symbol, then cast UnitValue<Unit> to UnitValue<A> (known from a MappableClass field declaration)@overrideUnitValue<A> decode<AextendsUnit>(value) =>UnitValue(_unitRegistry.where((u) => value[_unitKey] == u.symbol).single, value[_valueKey]).cast<A>();
@overrideJsonencode<AextendsUnit>(UnitValue self) => {_unitKey: self.unit.symbol, _valueKey: self.value};
@overrideFunctionget typeFactory =><TextendsUnit>(dynamic f) =>f<UnitValue<T>>();
}
There might be a simpler way to cause this to occur but the below code triggers it.
Removing TypePlus.add<Parent>() will cause all of the mappers to throw the assertion error Failed assertion: line 51 pos 12: 'context.args.length == 1': is not true,
Removing either of the TypePlus.add<ChildX>() lines will cause it's respective mapper to throw MapperException: Failed to encode (Container<Child1>): type 'dynamic' is not a subtype of type 'Parent' of 'A'
Minimal Example
import'package:dart_mappable/dart_mappable.dart';
import'package:type_plus/type_plus.dart';
classParent {
finalString type;
Parent(this.type);
}
classChild1extendsParent {Child1(super.type);}
classChild2extendsParent {Child2(super.type);}
classContainer<TextendsParent> {
Container(this.object);
finalT object;
}
classContainerMapperextendsSimpleMapper1Bounded<Container, Parent> {
final _key ='name';
@overrideContainer<Parent> decode<AextendsParent>(dynamic value) {
final str = value[_key];
returnswitch (str) {
'parent'=>Container(Parent(value)),
'child1'=>Container(Child1(value)),
'child2'=>Container(Child2(value)),
_ =>throwUnimplementedError()
};
}
@overrideObject?encode<AextendsParent>(covariantContainer<Parent> self) => {_key: self.object.type};
@overrideFunctionget typeFactory =><TextendsParent>(dynamic f) =>f<Container<T>>();
}
voidmain(List<String> arguments) {
MapperContainer.globals.use(ContainerMapper());
final p =Container(Parent('parent'));
TypePlus.add<Parent>();
final c1 =Container(Child1('child1'));
TypePlus.add<Child1>();
final c2 =Container(Child2('child2'));
TypePlus.add<Child2>();
final pJson =MapperContainer.globals.toJson(p);
final c1Json =MapperContainer.globals.toJson(c1);
final c2Json =MapperContainer.globals.toJson(c2);
print(pJson);
print(c1Json);
print(c2Json);
}
</details>
The text was updated successfully, but these errors were encountered:
I think thats expected because neither Parent nor ChildX are @MappableClass types, so dart_mappable (or in extension type_plus) cannot know about them automatically.
I understand why it happens but maybe an extension method in the mapper container would be handy (something like MapperContainer.globals.registerType<T>()), along with some documentation mentioning this. It was a bit difficult to track down and having to import TypePlus just to add it feels a bit hacky.
This is an odd one I've come across recently using the generic classes.
A solution seems to be to adding all possible subclasses to TypePlus using
TypePlus.add()
.My software uses this for unit conversions and uses a type parameter for Weight, Length, Angle (lots of these) etc to ensure that you can't subtract incorrect units,
My classses
There might be a simpler way to cause this to occur but the below code triggers it.
Removing
TypePlus.add<Parent>()
will cause all of the mappers to throw the assertion errorFailed assertion: line 51 pos 12: 'context.args.length == 1': is not true
,Removing either of the
TypePlus.add<ChildX>()
lines will cause it's respective mapper to throwMapperException: Failed to encode (Container<Child1>): type 'dynamic' is not a subtype of type 'Parent' of 'A'
Minimal Example
The text was updated successfully, but these errors were encountered: