createInstanceWithBestConstructor method

O? createInstanceWithBestConstructor(
  1. Map<String, Object?> map, {
  2. FieldNameResolver? fieldNameResolver,
  3. FieldValueResolver? fieldValueResolver,
  4. bool? allowEmptyConstructors,
  5. bool? allowOptionalOnlyConstructors,
})

Creates an instance with the constructor returned by getBestConstructorForMap, 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 createInstanceWithConstructors.

Implementation

O? createInstanceWithBestConstructor(
  Map<String, Object?> map, {
  FieldNameResolver? fieldNameResolver,
  FieldValueResolver? fieldValueResolver,
  bool? allowEmptyConstructors,
  bool? allowOptionalOnlyConstructors,
}) {
  allowOptionalOnlyConstructors ??= map.isEmpty;
  allowEmptyConstructors ??= map.isEmpty;

  var constructors = getBestConstructorsForMap(map,
      fieldNameResolver: fieldNameResolver,
      fieldValueResolver: fieldValueResolver,
      allowEmptyConstructors: allowEmptyConstructors,
      allowOptionalOnlyConstructors: allowOptionalOnlyConstructors);

  return createInstanceWithConstructors(constructors, map,
      fieldNameResolver: fieldNameResolver,
      fieldValueResolver: fieldValueResolver);
}