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

135

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

Share:
135
Dalon
Author by

Dalon

Updated on January 01, 2023

Comments

  • Dalon
    Dalon over 1 year

    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.
    
    • Jahidul Islam
      Jahidul Islam over 2 years
      what was the data type of your weightChart
    • Dalon
      Dalon over 2 years
      I fixed the issue: I used <double?> but it needs to be <double>