get<T> method
Returns object of requested type by given key or by Type from ControlFactory.
Given args are passed to Initializable.init if new instance of object is constructed. New instances always call Initializable.init, if implements.
When factory already contains requested object, Initializable.init is called only when args are set.
If object is not found in internal store, then factory tries to initialize new one via ControlFactory.init. When initialized object is LazyControl then this object is stored into Factory.
Returns concrete object or null.
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) {
_init(
item,
args: args,
forceInit: false,
);
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) {
_init(
item,
args: args,
forceInit: false,
);
return item;
}
}
final item = init<T>(
key: (useExactKey && key is Type) ? key : null,
args: args,
forceInit: true,
);
if (item is LazyControl) {
if (item.factoryKey != null) {
key = item.factoryKey;
} else {
item._factoryKey = key;
}
set<T>(key: key, value: item);
}
return item;
}