无法推断类型参数 'T'。使用 Map.reduce(max) 时
Couldn't infer type parameter 'T'. when using Map.reduce(max)
我想从列表中获取最大值。但是自从我迁移到空安全之后,我的代码就不再工作了:
weightChart = [45.3, 21.5, 521.3];
weightChart.reduce(max)
我收到错误:
Couldn't infer type parameter 'T'. Tried to infer 'double?' for 'T' which doesn't work: Type
parameter 'T' is declared to extend 'num' producing 'num'. The type 'double?' was inferred from:
Function type declared as 'T Function<T extends num>(T, T)' used where 'double? Function(double?,
double?)' is required. Consider passing explicit type argument(s) to the generic.
我们来试试
import 'dart:math';
void main() {
List<double> weightChart = [45.3, 21.5, 521.3];
print(weightChart.reduce(max));
}
我想从列表中获取最大值。但是自从我迁移到空安全之后,我的代码就不再工作了:
weightChart = [45.3, 21.5, 521.3];
weightChart.reduce(max)
我收到错误:
Couldn't infer type parameter 'T'. Tried to infer 'double?' for 'T' which doesn't work: Type
parameter 'T' is declared to extend 'num' producing 'num'. The type 'double?' was inferred from:
Function type declared as 'T Function<T extends num>(T, T)' used where 'double? Function(double?,
double?)' is required. Consider passing explicit type argument(s) to the generic.
我们来试试
import 'dart:math';
void main() {
List<double> weightChart = [45.3, 21.5, 521.3];
print(weightChart.reduce(max));
}