【问题标题】:Couldn't infer type parameter 'T'. when using Map.reduce(max)无法推断类型参数“T”。使用 Map.reduce(max) 时
【发布时间】:2021-10-08 10:27:53
【问题描述】:

我想从列表中获取最大值。但是自从我迁移到 null 安全后,我的代码不再工作了:

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.

【问题讨论】:

  • 你的weightChart的数据类型是什么
  • 我解决了这个问题:我使用了 但它必须是

标签: flutter dart mapreduce dart-null-safety


【解决方案1】:

我们试试

import 'dart:math';

void main() {
  List<double> weightChart = [45.3, 21.5, 521.3];
  print(weightChart.reduce(max));
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-05
    • 2014-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多