提问者:小点点

Flutter for每种类型:无法键入forach回调(参数类型N不能分配给参数类型“void Function(动态)”)


新手问题:

如果我不指定类型,它会说尝试添加显式类型,或者从分析选项文件中删除隐式动态(implicit_dynamic_parameter)

当我在函数参数前面添加类型double时,它说参数类型'void Function(double)'不能分配给参数类型'void Function(动态)'

另外我真的不明白为什么类型不是从上面的数组声明中推断出来的?

final List strengths = <double>[.05]; // type of array items in a forEach loop seems obvious

我尝试了很多不同的东西和语法,我在互联网上随处可见,但解决我的问题的唯一方法是删除analysis_options. yaml

我肯定我真的错过了显而易见的…你能给我指出正确的方向吗?

我看到的其他相关问题,无法适应我的案例:

  • Flutter DART"尝试添加显式类型,如“动态”,或在分析选项文件中启用隐式动态。"
  • 参数类型“Function”不能分配给参数类型“void Function()?”空安全后
  • Flutter-函数类型的参数不能分配给类型为“void函数()”的参数

真正的函数代码:

MaterialColor createMaterialColor(Color color) {
  final List strengths = <double>[.05];
  final swatch = <int, Color>{};
  final int r = color.red, g = color.green, b = color.blue;

  for (int i = 1; i < 10; i++) {
    strengths.add(0.1 * i);
  }
  strengths.forEach((double strength) {
    final double ds = 0.5 - strength;
    swatch[(strength * 1000).round()] = Color.fromRGBO(
      r + ((ds < 0 ? r : (255 - r)) * ds).round(),
      g + ((ds < 0 ? g : (255 - g)) * ds).round(),
      b + ((ds < 0 ? b : (255 - b)) * ds).round(),
      1,
    );
  });
  return MaterialColor(color.value, swatch);
}

共2个答案

匿名用户

像这样声明您的列表:

final List<double> strengths = <double>[.05];

匿名用户

只需从第一行中删除List关键字

之前

final List strengths = <double>[.05];

之后

final strengths = <double>[.05];