简体   繁体   English

可空表达式不能用作条件。 在将其用作条件之前,请尝试检查该值是否不是“空”。 flutter /飞镖

[英]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.

相关问题 “不能将可为 null 的表达式用作条件。\n在将其用作条件之前,请尝试检查该值是否为‘null’ - "A nullable expression can't be used as a condition.\nTry checking that the value isn't 'null' before using it as a condition 可空表达式不能用作 for-in 循环中的迭代器。 在将其用作迭代器之前尝试检查该值是否为“null” - A nullable expression can't be used as an iterator in a for-in loop. Try checking that the value isn't 'null' before using it as an iterator 带有条件的下拉列表。 颤振/飞镖 - Dropdown list with condition. Flutter/Dart 可空表达式不能用作条件 - A nullable expression can't be used as a condition 如何解决与 null 安全相关的问题:可空表达式不能用作条件 - how can sollve Problem related to null safety: A nullable expression can't be used as a condition 如何修复可空表达式不能用作条件错误 - How to fix a nullable expression can't be used as a condition error 如何使用 .where(GreaterThan, and LessThan) 条件从 firestore 获取数据。 - 颤振 - How to fetch data from firestore using .where(GreaterThan, and LessThan) condition. - Flutter 为什么此 Dart 代码给出“操作数不能为空,因此条件始终为真。删除条件”? - why does this Dart code give "The operand can't be null, so the condition is always true. Remove the condition"? 必须先分配不可为空的局部变量“docRef”,然后才能使用它。 尝试给它一个初始化表达式 flutter - The non-nullable local variable 'docRef' must be assigned before it can be used. Try giving it an initializer expression flutter 名称“任务”不是类型,因此不能用作类型参数。 | 颤振与飞镖 - The name 'Task' isn't a type so it can't be used as a type argument. | Flutter & Dart
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM