createNewInstance<T extends Object> function
T
createNewInstance<
T extends Object>( - Type classType
)
Implementation
T createNewInstance<T extends Object>(Type classType) {
final classMirror = reflectType(classType);
final constructorMethod = classMirror.declarations.entries
.firstWhereOrNull((e) => e.key == '$classType')
?.value as r.MethodMirror?;
final constructorParameters = constructorMethod?.parameters ?? [];
if (constructorParameters.isEmpty) {
return classMirror.newInstance(unnamedConstructor, const []) as T;
}
final namedDeps = constructorParameters
.where((e) => e.isNamed)
.map((e) => (
name: e.simpleName,
instance: instanceFromRegistry(type: e.reflectedType)
))
.fold<Map<Symbol, dynamic>>(
{}, (prev, e) => prev..[Symbol(e.name)] = e.instance);
final dependencies = constructorParameters
.where((e) => !e.isNamed)
.map((e) => instanceFromRegistry(type: e.reflectedType))
.toList();
return classMirror.newInstance(unnamedConstructor, dependencies, namedDeps)
as T;
}