addWeakReference method

bool addWeakReference({
  1. required Object instance,
  2. String? instanceId,
  3. required void onFinalize(
    1. String instanceId
    ),
})

Add a new instance with instanceId as key and instance as the value.

instance is stored as a weak reference and onFinalize is called after instance is garbage collected.

Returns true if the instance is successfully added. Returns false if the instanceId or instance is already contained in the manager or the instance is a num, bool, or String.

Implementation

bool addWeakReference({
  required Object instance,
  String? instanceId,
  required void Function(String instanceId) onFinalize,
}) {
  if (!_isValidInstance(instance)) return false;

  final String newId = instanceId ?? generateUniqueInstanceId(instance);

  _instanceIds[instance] = newId;
  _weakReferenceCallbacks[newId] = onFinalize;
  return _weakReferences.put(newId, instance);
}