You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter使用Visibility条件控制组件显示时出现类型不匹配相等判断错误如何解决

错误原因

你为ToolSetButton定义的buttonType属性是List类型,但判断逻辑里直接将这个列表类型变量和字符串'Half'做相等比较,两个完全无关的类型使用==运算符时,Dart的类型校验就会抛出你遇到的这个错误。

修复步骤
  • 修正buttonType的属性类型,你实际需要的是单个字符串类型的按钮类型标识,而非列表:
    原代码final List buttonType;修改为final String buttonType;
  • 修正构造函数的默认值配置,不需要默认赋值为整个类型列表,按照你的业务需求设置默认按钮类型即可:
    原代码this.buttonType = buttonTypeList修改为this.buttonType = 'FullButton'(也可以根据需求替换为'Half'或'Quarter')
  • 优化可见性判断逻辑,不需要额外使用三元运算符,直接返回布尔判断结果即可:
    原代码visible: widget.buttonType == 'Half' ? false : true简化为visible: widget.buttonType != 'Half'
修正后参考代码
const List<String> buttonTypeList = ['FullButton','Half','Quarter'];

class ToolSetButton extends StatefulWidget {
  final String buttonType;

  const ToolSetButton({
    Key? key,
    this.buttonType = 'FullButton'
  }) : super(key: key);

  // 其他业务逻辑代码...
  Visibility(
    visible: widget.buttonType != 'Half',
    child: SizedBox()
  )
}

内容的提问来源于stack exchange,提问作者Pretty_Girl

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 02:06:00