Polymorphism topic
A common pattern that you might want to use for your classes is polymorphism. As a simple example see the classes below.
abstract class Animal {
String name;
Animal(this.name);
}
class Cat extends Animal {
String color;
Cat(String name, this.color) : super(name);
}
class Dog extends Animal {
int age;
Dog(String name, this.age) : super(name);
}
Here the abstract Animal class can either be a Cat or a Dog which can inherit some properties but
also introduce new ones. Now consider the following class:
class Home {
Animal pet;
Home(this.pet);
}
Now when we want to encode a Home object, the pet property can either be a Cat or a Dog. Here
two problems arise:
- When deserializing, we need to make sure that the correct subclass is chosen and instantiated, and
- when serializing, we need to make sure that the subclass information isn't lost.
To solve these, we add a discriminator property, that keeps track of the specific (sub)-type of the pet.
By default, no discriminator is applied, but you can change this by setting the discriminatorKey annotation property (or globally in the build configuration).
The value of this property will default to the name of the class, but you can change this as well with the discriminatorValue annotation property.
To reiterate: Make sure to specify the discriminatorKey property globally or on the base class - Animal in our example - and the discriminatorValue property
on each of the child classes - Cat and Dog in our case.
// Set the discriminator key to "type".
@MappableClass(discriminatorKey: 'type')
abstract class Animal with AnimalMappable {
...
}
// Set the discriminator value for Cat to "kitty".
@MappableClass(discriminatorValue: 'kitty')
class Cat extends Animal with CatMappable {
...
}
// Here the other sub-class Dog will have the default discriminator value "Dog".
void main() {
// encode a polymorphic class
String catJson = Cat('Judy', 'black').toJson();
print(catJson); // {"name":"Judy","color":"black","type":"kitty"}
Animal myPet = AnimalMapper.fromJson(catJson); // implicit serialization as an 'Animal'
print(myPet.runtimeType); // has correct runtime type 'Cat'
// explicit serialization also works as usual without a discriminator
Cat myCat = CatMapper.fromJson('{"name": "Mindy", "color": "Brown"}');
print(myCat.name); // Mindy
}
Include Subclasses
The only slight downside of this system is that all sub-classes must be known statically at compile-time.
dart_mappable handles this in the following ways:
-
When a subclass is defined in the same library as the parent class it will be automatically included.
-
You can explicitly add a subclass from another library using the
includeSubClassesproperty of the@MappableClass()annotation:// Suppose [Hamster] is defined in some other library than [Animal] @MappableClass(discriminatorKey: 'type', includeSubClasses: [Hamster]) abstract class Animal with AnimalMappable { ... } -
Alternatively you can initialize a subclass yourself using
MySubClassMapper.ensureInitialized()and it will register itself with the superclass.
Null and Default Values
There are two additional cases that you might want to cover. Either your discriminator property is null, or some other value that you did not specify.
- For the
nullcase, you can explicitly set thediscriminatorValueproperty tonulland this will work as expected. - For the other case, you can specify a fallback class to be used whenever we come across an unknown discriminator value. On this class set the
discriminatorValuetoMappableClass.useAsDefault.
// This sub-class will be chosen when the discriminator is null.
@MappableClass(discriminatorValue: null)
class NullAnimal extends Animal with NullAnimalMappable {
NullAnimal(String name) : super(name);
}
// This sub-class will be chosen on any unknown discriminator.
@MappableClass(discriminatorValue: MappableClass.useAsDefault)
class DefaultAnimal extends Animal with DefaultAnimalMappable {
String type;
DefaultAnimal(String name, this.type) : super(name);
}
void main() {
// decode json with the discriminator set to null (same as missing property)
Animal animal1 = AnimalMapper.fromJson('{"name": "Scar", "type": null}');
print(animal1.runtimeType); // NullAnimal
// decode json with unknown discriminator value
Animal animal2 = AnimalMapper.fromJson('{"name": "Balu", "type": "Bear"}');
print(animal2.runtimeType); // DefaultAnimal
print(animal2.type); // Bear
}
Multiple Constructors
You can also use factory constructors on the base class to provide a more convenient API for creating instances.
// Set the discriminator key to "type".
@MappableClass(discriminatorKey: 'type')
abstract class Animal with AnimalMappable {
const Animal(this.name);
// Factory constructors pointing to subclasses
const factory Animal.nullAnimal(String name) = NullAnimal;
const factory Animal.defaultAnimal(String name, String type) = DefaultAnimal;
final String name;
}
Custom Discriminator Logic
For a more advanced use-case where the discriminator key/value system is not enough, you can also
use a custom predicate function as the discriminatorValue. This function can check whether the
encoded value should be decoded to this subclass and return a boolean.
@MappableClass()
abstract class A with AMappable {
A();
}
@MappableClass(discriminatorValue: B.checkType)
class B extends A with BMappable {
B();
/// checks if [value] should be decoded to [B]
static bool checkType(value) {
return value is Map && value['isB'] == true;
}
}
@MappableClass(discriminatorValue: C.checkType)
class C extends A with CMappable {
C();
/// checks if [value] should be decoded to [C]
static bool checkType(value) {
return value is Map && value['isWhat'] == 'C';
}
}
Implements
You can use implements for polymorphism through a single mappable interface. Put
@MappableClass and the generated mixin on the interface and on each implementing class,
and set discriminatorKey on the interface.
You cannot pass fields with super. when using implements, so redeclare them on the subclass.
@MappableClass(discriminatorKey: 'type')
abstract class Animal with AnimalMappable {
Animal(this.name);
final String name;
}
@MappableClass()
class Cat with CatMappable implements Animal {
Cat(this.name, this.color);
@override
final String name;
final String color;
}
@MappableClass(discriminatorValue: 'puppy')
class Dog with DogMappable implements Animal {
Dog(this.name, this.age);
@override
final String name;
final int age;
}
void main() {
String catJson = Cat('Judy', 'black').toJson();
print(catJson); // {"name":"Judy","color":"black","type":"Cat"}
Animal myPet = AnimalMapper.fromJson(catJson);
print(myPet.runtimeType); // Cat
}
includeSubClasses, custom discriminatorValues, and factory constructors on the interface all
apply the same as with extends. The generator automatically registers a discriminating subclass
with one base: its mappable superclass, or the first mappable interface if it has no mappable
superclass.
Mixins (with)
Every mappable class already uses with ClassMappable. That generated mixin is only there for
toJson, copyWith, equality, and so on. It is not a polymorphic parent.
A user-defined mixin is also not a polymorphic parent. The builder only follows extends and
implements, so a mixin cannot be a discriminator base. Use the mixin for shared fields or
behavior, and put those fields on the class constructor so they are serialized.
Apply your mixins first and keep the generated *Mappable mixin last:
mixin Named {
String get name;
}
@MappableClass()
class Person with Named, PersonMappable {
Person(this.name, this.age);
@override
final String name;
final int age;
}
Do not put @MappableField on the mixin (or on an interface constructor). Annotate the
constructor parameter or field on the mappable class itself.
Mixins and Implements together
To share a mixin and decode through an interface, combine both:
mixin Named {
String get name;
}
@MappableClass(discriminatorKey: 'type')
abstract class Animal with AnimalMappable {
String get name;
}
@MappableClass()
class Cat with Named, CatMappable implements Animal {
Cat(this.name, this.color);
@override
final String name;
final String color;
}
If you need a polymorphic hierarchy, prefer extends or implements. Use with for extra
behavior on top of that.
Classes
- MappableClass Introduction Models Configuration Polymorphism Mapping Hooks Custom Mappers
- Used to annotate a class in order to generate mapping code.
- MappableLib Configuration Polymorphism Generics
- Used to annotate a library to define default values.