constructorParametersSameType function

String constructorParametersSameType(
  1. ConstructorElement constructor, {
  2. Set<DartType>? skip,
})

Implementation

String constructorParametersSameType(
  final ConstructorElement constructor, {
  Set<DartType>? skip,
}) {
  String arg = "", superArg = "", storeing = "";
  bool hasNamed = false;

  List<String> sharedInstances = [];

  for (var par in constructor.parameters) {
    if (skip?.contains(par.type) ?? false) {
      String type = par.type.toString();

      if (type[type.length - 1] == "?") {
        sharedInstances.add("${par.type} ${par.name}_isolate,");
      } else {
        sharedInstances.add("${par.type}? ${par.name}_isolate,");
      }

      superArg += par.isNamed
          ? "${par.name}: ${par.name}_isolate!,"
          : "${par.name}_isolate!,";

      continue;
    }

    superArg += par.isNamed ? "${par.name}: ${par.name}," : "${par.name},";

    if (par.isNamed && !hasNamed) {
      hasNamed = true;
      arg += "{";
    }

    arg +=
        "${(hasNamed && par.isRequired) ? 'required ' : ''}${par.type} ${par.name}${par.hasDefaultValue ? ' = ${par.defaultValueCode}' : ''},";

    storeing += "_${par.name} = ${par.name},";
  }

  if (sharedInstances.isNotEmpty) {
    if (!hasNamed) {
      arg += "{";
      hasNamed = true;
    }

    arg += sharedInstances.join("");
  }

  if (hasNamed) arg += "}";

  if (arg.isNotEmpty) return "($arg): $storeing super($superArg)";

  return "()";
}