有条件地执行功能,抛出错误"条件必须具有静态类型的"bool"。尝试更改条件'



我想把Two喂给function。当函数接收到Two作为数据时,应该将success打印到控制台。但这似乎不是正确的方法。

错误:Conditions must have a static type of 'bool'. Try changing the condition.

enum ButtonList {One, Two, Three}

调用函数

testFunc(ButtonList.Two)),

testFunc( ButtonList type) {
if (type = ButtonList.Two ){print('sucess ')};

}

应该是:

testFunc(ButtonList type) {
if (type == ButtonList.Two) {
print('sucess ')
};  
}

=(给变量赋值)和==(相等比较)之间有很大的区别。if需要一个条件(==),而不是赋值操作(=)。

格式化对于阅读和理解代码很重要。请阅读Dart最佳代码风格实践:https://dart.dev/guides/language/effective-dart/style

如果您想用=赋值,请使用==代替

最新更新