flutter - 'bool?' 类型的值 不能分配给“bool”类型的变量。小部件返回值
问题描述
我有一个声明
bool _glutenFree = false;
当我尝试使用如下赋值运算符时
_glutenFree = widget.currentFilters['gluten'];
我收到一条错误消息
'bool?' 类型的值 不能分配给“bool”类型的变量。
检查“widget.currentFilters['gluten']”的值
我尝试使用 print 命令,发现该值为 false。
现在,当我将赋值语句更改为
_glutenFree = widget.currentFilters['gluten'] == true;
该程序有效。
你能告诉我这个问题吗
解决方案
我自己尝试了一些解决方案。如果我!
在最后添加:
_glutenFree = widget.currentFilters['gluten']!
代码会崩溃。
但
_glutenFree = widget.currentFilters['gluten'] ?? false;
是您问题的最佳答案。
如果你使用布尔?_glutenfree 也很好
推荐阅读
- c# - “类型不包含类的定义”错误,但它完全没有
- javascript - 开玩笑地测试事件侦听器的最简单方法是什么?
- r - r 代码,尽管有行,但只评估一个值
- java - Java,将控制台内容写入文件
- python - pygame中的弹丸运动和重力仅有时有效
- reactjs - 如何处理 React 生命周期方法中的 API 调用?
- java - Convert Sting of comma delimited into Array using Stream
- c - 除了(可能)内存使用之外,将 uint32 换成 uint64 是良性的吗?
- reactjs - 内联 css 属性 backgroundImage 不会在子目录 ReactJS 中的构建上传时加载图像
- javascript - Mongoose 模式不会创建具有给定属性的正确对象