typeNameOf function Null safety

String typeNameOf(
  1. DartType type
)

Returns a non-null name for the provided type.

In newer versions of the Dart analyzer, a typedef does not keep the existing name, because it is used an alias:

// Used to return `VoidFunc` for name, is now `null`.
typedef VoidFunc = void Function();

This function will return 'VoidFunc', unlike DartType.element.name.

Implementation

String typeNameOf(DartType type) {
  final aliasElement = type.aliasElement;
  if (aliasElement != null) {
    return aliasElement.name;
  }
  if (type is DynamicType) {
    return 'dynamic';
  }
  if (type is InterfaceType) {
    return type.element.name;
  }
  if (type is TypeParameterType) {
    return type.element.name;
  }
  throw UnimplementedError('(${type.runtimeType}) $type');
}