Home > OS >  Couldn't infer type parameter 'T'. when using Map.reduce(max)
Couldn't infer type parameter 'T'. when using Map.reduce(max)

Time:10-08

I want to get the max value from a List. But since i migrated to null safety my code doesnt work anymore:

weightChart = [45.3, 21.5, 521.3];
weightChart.reduce(max)

I get the error:

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.

CodePudding user response:

Let's try

import 'dart:math';

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

enter image description here

  • Related