在 Dart 语言中使用实验性扩展方法时的类型推断

Type inference when using experimental extension methods in the Dart language

当我让Dart分析下面的例子时...

extension MyList<T> on List<T> {
  List<T> mapToList<T>(T Function(T) convert) => this.map<T>(convert).toList();
}

... dartanalyzer 在转换声明 "The argument type 'T Function(T)' can't be assigned to the parameter type 'T Function(T)'.dart(argument_type_not_assignable)" 时报告类型错误。在提交问题之前,我想了解为什么我犯了错误。

地图方法定义为 Iterable<T> map<T>(T f(E e)) => MappedIterable<E, T>(this, f); 其中E是列表的类型,可以和列表的类型不同。 所以你必须做的是:

extension MyList<E> on List<E> {
  List<T> mapToList<T>(T convert(E e)) => this.map<T>(convert).toList();
}