createInstanceWithConstructors method

O? createInstanceWithConstructors(
  1. List<ConstructorReflection<O>> constructors,
  2. Map<String, Object?> map, {
  3. FieldNameResolver? fieldNameResolver,
  4. FieldValueResolver? fieldValueResolver,
})

Creates an instance with one of the constructors, using map entries as parameters.

  • Throws UnresolvedParameterError when it's its impossible to call any constructor due to unresolved parameters.
  • Throws any error thrown by a constructor invocation.

See createInstanceWithBestConstructor.

Implementation

O? createInstanceWithConstructors(
  List<ConstructorReflection<O>> constructors,
  Map<String, Object?> map, {
  FieldNameResolver? fieldNameResolver,
  FieldValueResolver? fieldValueResolver,
}) {
  final length = constructors.length;

  if (length == 0) {
    return null;
  } else if (length == 1) {
    return _createInstanceWithConstructorImpl(constructors.first, map,
        fieldNameResolver: fieldNameResolver,
        fieldValueResolver: fieldValueResolver);
  } else if (constructors is ListSortedByUsage<ConstructorReflection<O>>) {
    var sorted = constructors.sortedByUsage();
    var ret = _createInstanceWithConstructorsImpl(sorted, map,
        fieldNameResolver: fieldNameResolver,
        fieldValueResolver: fieldValueResolver);
    if (ret != null) {
      constructors.notifyUsage(ret.$1);
      return ret.$2;
    }
    return null;
  } else {
    var ret = _createInstanceWithConstructorsImpl(constructors, map,
        fieldNameResolver: fieldNameResolver,
        fieldValueResolver: fieldValueResolver);
    return ret?.$2;
  }
}