[英]A nullable expression can't be used as a condition. Try checking that the value isn't 'null' before using it as a condition. flutter /dart
I've got a map which contains (4 keys and 4 values) as shown in the code below:我有一个 map 包含(4 个键和 4 个值),如下面的代码所示:
class _MyAppState extends State<MyApp> {
Map<String, bool> _filters = {
'gluten': false,
'lactose': false,
'vegan': false,
'vegetarian': false
};
In order to filter the map to meet certain conditions, I've created a _setFilters Method as shwon below:为了过滤 map 以满足某些条件,我创建了一个 _setFilters 方法,如下所示:
List<Meal> _availabaleMeals = DUMMY_MEALS;
void _setFilters(Map<String, bool> filterData){
setState(() {
_filters = filterData;
_availabaleMeals = DUMMY_MEALS.where((meal){
if (_filters['gluten']&& !meal.isGlutenFree) {
return false;
}
if (_filters['lactode']e && !meal.isLactoseFree) {
return false;
}
if (_filters['vegan'] && !meal.isVegan) {
return false;
}
if (_filters['vegetarian'] && !meal.isVegetarian) {
return false;
}
}).toList();
});
}
The problem is dart doesn't accept the if conditions exactly here:问题是 dart 不完全接受 if 条件:
if (_filters['gluten']&& !meal.isGlutenFree) {
return false;
}
if (_filters['lactode']e && !meal.isLactoseFree) {
return false;
}
if (_filters['vegan'] && !meal.isVegan) {
return false;
}
if (_filters['vegetarian'] && !meal.isVegetarian) {
return false;
}
I'd really appreciate your help guys我真的很感谢你的帮助
You've not showed your "Meal" class here, but if the values are nullable, meaning you've declared properties in it with a "?"您还没有在此处显示您的“膳食”class,但如果这些值可以为空,这意味着您已经使用“?”声明了其中的属性。 or not made them required, you're going to have to make sure they are actually not null.
或不要求它们,您将必须确保它们实际上不是 null。
For example:例如:
if (meal.isLactoseFree != nulL && _filters['lactode'] && !meal.isLactoseFree)
Another very real possibility is that the trailing "e" you have in your second if statement behind "_filters['lactode']" is causing the issue.另一个非常真实的可能性是,您在“_filters['lactode']”后面的第二个 if 语句中的尾随“e”导致了这个问题。 Lactose is also misspelled in that instance, by the way.
顺便说一句,乳糖在这种情况下也拼错了。 Consider using constants if you are going to be referring to keys in multiple places.
如果您要在多个位置引用键,请考虑使用常量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.