Home > Mobile >  why the filter does not work as expected?
why the filter does not work as expected?

Time:10-05

how can I make the logic so when I press vegan and lactose it will show me a meal that contains at least one of them or both? in the current state it shows only meals that contains both.


Map<String, bool> _filters = {
    'gluten': false,
    'lactose': false,
    'vegan': false,
    'vegetarian': false,
  };

//above is a switchTile so the values change


    _availableMeals = DUMMY_MEALS.where((meal) {
            if (_filters['gluten']! && !meal.isGlutenFree) {
              return false;
            }
            if (_filters['lactose']! && !meal.isLactoseFree) {
              return false;
            }
            if (_filters['vegan']! && !meal.isVegan) {
              return false;
            }
            if (_filters['vegetarian']! && !meal.isVegetarian) {
              return false;
            }
            return true;
          }).toList();

CodePudding user response:

Here you go. Part of the problem was that you were using ! to mean == false

  _availableMeals = DUMMY_MEALS.where((meal) {
    

    // actually i'm not sure about this part, try removing it if it doesn't make sense in your UI
    if (_filters['gluten'] == false &&
        _filters['lactose'] == false &&
        _filters['vegan'] == false &&
        _filters['vegatarian'] == false) {
      return true;
    }

    if (_filters['gluten'] == true && meal.isGlutenFree) {
      return true;
    }
    if (_filters['lactose'] == true && meal.isLactoseFree) {
      return true;
    }
    if (_filters['vegan'] == true && meal.isVegan) {
      return true;
    }
    if (_filters['vegetarian'] == true && meal.isVegetarian) {
      return true;
    }
    
    return false;
  }).toList();
  
  • Related