unnamedConstructor method

ConstructorElement? unnamedConstructor(
  1. ClassElement element
)

Finds the unnamed constructor if it is present.

Otherwise, use the first encountered.

Implementation

ConstructorElement? unnamedConstructor(ClassElement element) {
  ConstructorElement constructor;
  final constructors = element.constructors.cast<ConstructorElement>();
  if (constructors.isEmpty) {
    throw BuildError.forElement(element, 'No constructors found');
  }

  constructor = constructors.firstWhere(
      (constructor) => constructor.name.isEmpty,
      orElse: () => constructors.first);

  if (constructor.isPrivate) {
    throw BuildError.forElement(element, 'No constructors found');
  }

  if (element.isAbstract && !constructor.isFactory) {
    logWarning(
        'Found a constructor for abstract class ${element.name} but it is '
        'not a "factory", and cannot be invoked');
    return null;
  }
  if (element.constructors.length > 1 && constructor.name.isEmpty) {
    // No use in being a warning, as it's not something they need to fix
    // until we add a way to be able to "pick" the constructor to use.
    logFine('Found ${element.constructors.length} constructors for class '
        '${element.name}; using constructor ${constructor.name}.');
  }
  return constructor;
}