convertToPrimitiveValue method Null safety

  1. @override
dynamic convertToPrimitiveValue(
  1. dynamic value
)
override

Converts a value from a more complex value into a primitive value according to this instance's definition.

This method takes a Dart representation of a value and converts it to something that can be used elsewhere (e.g. an HTTP body or database query). How this value is computed depends on this instance's definition.

Implementation

@override
dynamic convertToPrimitiveValue(dynamic value) {
  if (value is ManagedSet) {
    return value
        .map((ManagedObject innerValue) => innerValue.asMap())
        .toList();
  } else if (value is ManagedObject) {
    // If we're only fetching the foreign key, don't do a full asMap
    if (relationshipType == ManagedRelationshipType.belongsTo &&
        value.backing.contents!.length == 1 &&
        value.backing.contents!.containsKey(destinationEntity.primaryKey)) {
      return <String, dynamic>{
        destinationEntity.primaryKey!: value[destinationEntity.primaryKey]
      };
    }

    return value.asMap();
  } else if (value == null) {
    return null;
  }

  throw StateError(
    "Invalid relationship assigment. Relationship '$entity.$name' is not a 'ManagedSet' or 'ManagedObject'.",
  );
}