addWeakReference method
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);
}