get<O> method
Returns an object for key
.
Uses instantiator
(or pre-defined) to instantiate.
Uses cacheValidator
(or pre-defined) to validade already cached instance.
Implementation
O? get<O>(String key,
[O Function()? instantiator, bool Function(Object o)? cacheValidator]) {
var o = _cacheInstances[key];
if (o != null) {
cacheValidator ??= _cacheValidators[key];
if (cacheValidator != null) {
var ok = cacheValidator(o);
if (ok) {
return o;
} else {
_cacheInstances.remove(key);
}
} else {
return o;
}
}
instantiator ??= _cacheInstantiators[key] as O Function()?;
if (instantiator == null) return null;
if (maxInstances != null && maxInstances! > 0) {
var needToRemove = _cacheInstances.length - (maxInstances! - 1);
disposeInstances(needToRemove);
}
o = instantiator();
_cacheInstances[key] = o;
return o;
}