Flutter forEach typing: Unable to type a foreach callback (参数类型AND不能分配给参数类型'void Function(dynamic)')
Flutter forEach typing: Unable to type a forach callback (The argument type N can't be assigned to the parameter type 'void Function(dynamic)')
这里的菜鸟问题总数:
如果我没有指定类型,它会说Try adding an explicit type, or remove implicit-dynamic from your analysis options file.dart(implicit_dynamic_parameter)
当我在函数参数前面添加类型double
时,它表示The argument type 'void Function(double)' can't be assigned to the parameter type 'void Function(dynamic)'
我也很不明白为什么不能从上面的数组声明推断出类型?
final List strengths = <double>[.05]; // type of array items in a forEach loop seems obvious
我尝试了很多在互联网上随处可见的不同的东西和语法,但解决我的问题的唯一方法是删除 analysis_options.yaml
我确定我真的错过了明显的...你能给我指明正确的方向吗?
其他我看到的不能适应我的相关问题:
真正的功能代码:
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);
}
只需删除第一行的 List
关键字
之前
final List strengths = <double>[.05];
之后
final strengths = <double>[.05];
像这样声明您的列表:
final List<double> strengths = <double>[.05];
这里的菜鸟问题总数:
如果我没有指定类型,它会说Try adding an explicit type, or remove implicit-dynamic from your analysis options file.dart(implicit_dynamic_parameter)
当我在函数参数前面添加类型double
时,它表示The argument type 'void Function(double)' can't be assigned to the parameter type 'void Function(dynamic)'
我也很不明白为什么不能从上面的数组声明推断出类型?
final List strengths = <double>[.05]; // type of array items in a forEach loop seems obvious
我尝试了很多在互联网上随处可见的不同的东西和语法,但解决我的问题的唯一方法是删除 analysis_options.yaml
我确定我真的错过了明显的...你能给我指明正确的方向吗?
其他我看到的不能适应我的相关问题:
真正的功能代码:
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);
}
只需删除第一行的 List
关键字
之前
final List strengths = <double>[.05];
之后
final strengths = <double>[.05];
像这样声明您的列表:
final List<double> strengths = <double>[.05];