简体   繁体   中英

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:

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:

 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:

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 "?" or not made them required, you're going to have to make sure they are actually not 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM