CheckTypesHook class Polymorphism Mapping Hooks

A MappingHook that allows to specify custom type checks for serialization polymorph subclasses of a class.

This overrides the default discriminator key/value system to allow for a more custom implementation.

To use this hook, specify a type predicate for each of your subtypes in the form of a static or top-level function of type bool Function(dynamic value). This function should return true if the encoded value should be decoded to the respective subtype. Then use this hook on the superclass and provide a map with the subtypes as keys and the predicate functions as values.

Example for superclass A and subclasses B and C (extends A):

@MappableClass(
  hooks: CheckTypesHook({
    B: B.checkType,
    C: C.checkType,
  }),
)
abstract class A with AMappable {
  A();
}

@MappableClass()
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()
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';
  }
}
Inheritance

Constructors

CheckTypesHook(Map<Type, bool Function(dynamic)> checks)
const

Properties

checks Map<Type, bool Function(dynamic)>
final
hashCode int
The hash code for this object.
no setterinherited
isLocked bool
getter/setter pair
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

afterDecode(dynamic value) → dynamic
inherited
afterEncode(dynamic value) → dynamic
inherited
beforeDecode(dynamic value) → dynamic
inherited
beforeEncode(dynamic value) → dynamic
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited
wrapDecode<T>(dynamic value, T fn(dynamic value), MapperContainer container) → T
override
wrapEncode<T>(T value, dynamic fn(T value), MapperContainer container) → dynamic
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited