target property

EntityT? get target

Get target object. If it's the first access, this reads from DB.

Implementation

EntityT? get target {
  if (_value._state == _ToOneState.lazy) {
    final configuration = _getStoreConfigOrThrow();
    var store =
        StoreInternal.attachByConfiguration(configuration.storeConfiguration);
    final EntityT? object;
    try {
      object = configuration.box(store).get(_value._id);
    } finally {
      store.close();
    }
    _value = (object == null)
        ? _ToOneValue<EntityT>.unresolvable(_value._id)
        : _ToOneValue<EntityT>.stored(_value._id, object);
  }
  return _value._object;
}
set target (EntityT? object)

Set relation target object. Note: this does not store the change yet, use Box.put() on the containing (relation source) object.

Implementation

set target(EntityT? object) {
  // If not attached, yet, avoid throwing and set the ID to unknown instead.
  // If the targetId getter is used later, it will call this to re-try
  // resolving the ID.
  if (object == null) {
    _value = _ToOneValue<EntityT>.none();
  } else if (_storeConfiguration != null) {
    final id = _getId(object);
    _value = (id == 0)
        ? _ToOneValue<EntityT>.unstored(object)
        : _ToOneValue<EntityT>.stored(id, object);
  } else {
    _value = _ToOneValue.unknown(object);
  }
}