get<T> method
Returns object of requested type by given key
or by Type from ControlFactory.
When args
are not empty and object is Initializable, then Initializable.init is called.
Set withInjector
to re-inject stored object.
If object is not found in internal store, then factory tries to initialize new one via ControlFactory.init.
When T
is passed to initializer and args
are null, then key
is used as arguments for ControlFactory.init.
And when initialized object is LazyControl then this object is stored into Factory.
Implementation
T? get<T>({dynamic key, dynamic args, bool withInjector = true}) {
final useExactKey = key != null;
key = keyOf<T>(key: key);
assert(key != null);
if (_items.containsKey(key)) {
final item = _items[key] as T?;
if (item != null) {
inject(item, args: args, withInjector: withInjector);
return item;
}
}
if (!useExactKey) {
T? item;
if (T != dynamic) {
item =
_items.values.firstWhere((item) => item is T, orElse: () => null);
} else if (key == Type) {
item = _items.values
.firstWhere((item) => item.runtimeType == key, orElse: () => null);
}
if (item != null) {
inject(item, args: args, withInjector: withInjector);
return item;
}
}
final item = init<T>(key: key == Type ? key : null, args: args ?? key);
if (item is LazyControl) {
if (item.factoryKey != null) {
key = item.factoryKey;
}
item._factoryKey = key;
set<T>(key: key, value: item);
}
return item;
}