getConstructorsMatchingFields method

Iterable<ConstructorElement> getConstructorsMatchingFields({
  1. required Iterable<FieldDescriptorBase> fieldDescriptors,
  2. bool allowMissingFields = false,
  3. Set<String> missingFields = const <String>{},
})

Gets all constructors that match a certain set of FieldDescriptors.

Optionally allow for final fields that are not present in the constructor. In that case it is up to the compiler to catch invalid constructor calls.

Implementation

Iterable<ConstructorElement> getConstructorsMatchingFields(
    {required Iterable<FieldDescriptorBase> fieldDescriptors,
    bool allowMissingFields = false,
    Set<String> missingFields = const <String>{}}) {
  final constructors =
      this.constructors.where((constructor) => fieldDescriptors.every((fd) {
            final match = !fd.isFinal ||
                fd.isNullable ||
                //fd.isLate ||
                constructor.parameters.any((cp) => cp.name == fd.name);
            if (!match) {
              missingFields.add(fd.displayName);
              if (allowMissingFields) {
                print('WARNING: missing field ${fd.displayName}');
                return true;
              }
            }
            return match;
          }));
  return constructors;
}