createInstanceWithBestConstructor method
O?
createInstanceWithBestConstructor(
- Map<
String, Object?> map, { - FieldNameResolver? fieldNameResolver,
- FieldValueResolver? fieldValueResolver,
Creates an instance with the constructor returned by getBestConstructorForMap,
using map
entries as parameters.
Implementation
O? createInstanceWithBestConstructor(Map<String, Object?> map,
{FieldNameResolver? fieldNameResolver,
FieldValueResolver? fieldValueResolver}) {
var constructors = getBestConstructorsForMap(map,
fieldNameResolver: fieldNameResolver,
fieldValueResolver: fieldValueResolver,
allowEmptyConstructors: map.isEmpty);
if (constructors.isEmpty) return null;
var invocationErrors = <List>[];
void catchInvokeError(
ConstructorReflection constructor,
MethodInvocation methodInvocation,
Map<String, dynamic> map,
Object? error) {
invocationErrors.add([constructor, methodInvocation, map, error]);
}
for (var c in constructors) {
var o = createInstanceWithConstructor(c, map,
fieldNameResolver: fieldNameResolver,
fieldValueResolver: fieldValueResolver,
onInvocationError: catchInvokeError);
if (o != null) return o;
}
if (invocationErrors.isNotEmpty) {
for (var i = 0; i < invocationErrors.length; ++i) {
var args = invocationErrors[i];
_showInvokeError(i, args[0], args[1], args[2], args[3]);
}
var error = invocationErrors.first[3];
throw error;
}
return null;
}