传入enum参数调用Dart函数报Conditions must have a static type of 'bool'错误如何解决
错误原因&修改方案
你遇到的报错是因为对应语言(Dart)语法中if判断的条件必须为布尔类型,你的代码共有3处需要调整:
- if判断语句内误用了赋值运算符
=,该操作的返回值是ButtonList枚举类型,不是布尔值,需要替换为判断相等的==运算符 - 函数调用代码末尾多了1个多余的右括号,直接删除即可
- 打印的字符串拼写有误,
sucess修正为success
修改后完整代码
枚举定义(无需修改)
enum ButtonList {One, Two, Three}
函数调用代码
testFunc(ButtonList.Two);
函数实现代码
testFunc(ButtonList type) { if (type == ButtonList.Two) { print('success'); } }
内容的提问来源于stack exchange,提问作者Pretty_Girl
相关产品推荐
相关产品推荐

