substituteType function

DartType substituteType(
  1. DartType type,
  2. Map<String, DartType> substitutions
)

Implementation

DartType substituteType(DartType type, Map<String, DartType> substitutions) {
  if (substitutions.isEmpty) {
    return type;
  }

  switch (type) {
    case TypeParameterType(:final element):
      final name = element.name;
      if (name == null || !substitutions.containsKey(name)) {
        return type;
      }

      return _withNullability(substitutions[name]!, type.nullabilitySuffix);

    case InterfaceType(:final element, :final typeArguments):
      if (typeArguments.isEmpty) {
        return type;
      }

      final substitutedArguments = typeArguments
          .map((argument) => substituteType(argument, substitutions))
          .toList(growable: false);

      if (_typesEqual(substitutedArguments, typeArguments)) {
        return type;
      }

      return element.instantiate(
        typeArguments: substitutedArguments,
        nullabilitySuffix: type.nullabilitySuffix,
      );

    default:
      return type;
  }
}