why the filter does not work as expected? - flutter

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();

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

flutter search in search

This is the onsearch function that searches for all custnum addresses.
onSearch(String text) async {
print(text);
List<Item> itemII = items;
if (text.isNotEmpty) {
List<Item> itemList = [];
for (var item in itemII) {
if (item.custnum == text.toLowerCase().toUpperCase()) {
itemList.add(item);
}
}
setState(() {
searchText = text;
searchitems.clear();
searchitems.addAll(itemList);
MainPage.searchItemMainPage.addAll(itemList);
if (searchitems.isNotEmpty) {
print({MainPage.searchItemMainPage[0].address});
searchitems[0].address!.sort((a, b) {
if (a.for_bill == 'Y' && b.for_bill == 'N') {
return -1;
} else if (a.for_bill == 'N' && b.for_bill == 'Y') {
return 1;
} else if (a.for_ship == 'Y' && b.for_ship == 'N') {
return -1;
} else if (a.for_ship == 'N' && b.for_ship == 'Y') {
return 1;
} else if (a.for_contact == 'Y' && b.for_contact == 'N') {
return -1;
} else if (a.for_contact == 'N' && b.for_contact == 'Y') {
return 1;
} else {
return 0;
}
});
}
});
} else {
setState(() {
searchCus.clear();
searchitems.clear();
MainPage.searchItemMainPage.clear();
// searchitems.addAll(items);
print('searchitems : $searchitems');
});
}
}
This is the onsearchaddr function, it finds the address in searchitems after the search. By typing the name of the field addr, then the address will be displayed.
onSearchAddr(String text) {
List<Item> result = searchitems;
if (text.isNotEmpty) {
List<Item> itemList = [];
for (var item in result) {
item.address = item.address!
.where((element) =>
element.addr1!.toUpperCase().toLowerCase().contains(text))
.toList();
itemList.add(item);
}
setState(() {
searchitems.clear();
searchitems.addAll(itemList);
print('itemList : ${itemList}');
});
} else {
setState(() {
searchitems.clear();
searchitems.addAll(MainPage.searchItemMainPage);
print(
'SearchitemsMainPage : ${MainPage.searchItemMainPage[0].address}');
});
}
}
Could you please write some code to modify onsearchaddr, it can search as typed but when it clears text it doesn't show information of searchitems as before but it shows information of last typed message.
It's like the previous backup wasn't provided.

How to check a group of text edit controllers to see if any of them are null?

I'd like to have a function that checks to see if any of the text edit controller fields are null.
I wrote this method below, but I wanted to see if there was a more elegant solution.
bool _nullfieldExists(){
//return a true if any of the textEditController fields are null
if (_textEditControllerNumberPlayers.text == null ){
return true;
}
else if (_textEditControllerSmallBlind.text == null ){
return true;
}
else if (_textEditControllerBigBlind.text == null ){
return true;
}
else if (_textEditControllerAnte.text == null ){
return true;
}
else if (_textEditControllerMyStack.text == null ){
return true;
}
else {
return false;
}
}

About mybatis3.3 databaseId,the code is useless

I study the mybatis3.3 sources code and now I have a question:in the XMLMapperBuilder.databaseIdMatchesCurrent()
private boolean databaseIdMatchesCurrent(String id, String databaseId, String requiredDatabaseId) {
if (requiredDatabaseId != null) {
if (!requiredDatabaseId.equals(databaseId)) {
return false;
}
} else {
if (databaseId != null) {
return false;
}
// skip this fragment if there is a previous one with a not null databaseId
if (this.sqlFragments.containsKey(id)) {
XNode context = this.sqlFragments.get(id);
if (context.getStringAttribute("databaseId") != null) {
return false;
}
}
}
return true;
}
if the (requiredDatabaseId == null && databaseId != null) ,then the function will return false.
So the code
if (this.sqlFragments.containsKey(id)) {
XNode context = this.sqlFragments.get(id);
if (context.getStringAttribute("databaseId") != null) {
return false;
}
}
certainly no way to return false and this code is useless.
I just want to questioning: I understand this, right?

My logic for seeing if a user has won a tic-tac-toe match isn't working [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
Below is some code to check if somebody has won on a tic tac toe board. (board[0] - board[8] represent the tic tac toe board from top to bottom, left to right):
func checkWin(board: [Int]) -> Bool{
if board[0] != 0 {
if board[0] == board[1] && board[1] == board[2] {
return true
} else if board[0] == board[3] && board[3] == board[6] {
return true
}
} else if board[4] != 0 {
if board[1] == board[4] && board[4] == board[7] {
return true
} else if board[3] == board[4] && board[4] == board[5] {
return true
} else if board[2] == board[4] && board[4] == board[6] {
return true
} else if board[0] == board[4] && board[4] == board[8] { //
return true
}
} else if board[8] != 0 {
if board[2] == board[5] && board[5] == board[8] { //
return true
} else if board[6] == board[7] && board[7] == board[8] { //
return true
}
}
return false
}
However, the three lines with // at the end will not return true even if the conditions are met. I have noticed that they all share board[8], however I know this is not a problem with the storyboard, as if I hard code a win that satisfies one of faulty conditions it still doesn't work. Can anyone see what's going wrong?
Your logic is flawed. Once you check with the outer ifs, you have committed to just checking a few of the possible cases.
For instance, if space 0 is not empty, then you are only checking cases 0-1-2 and 0-3-6, but you aren't checking 0-4-8 so you'll miss that possibility. The 0-4-8 case is handled inside the first else if case, but you'll never get there if board[0] != 0.
You can fix this by using 3 ifs instead of the else ifs.
func checkWin(board: [Int]) -> Bool{
print(board)
if board[0] != 0 {
if board[0] == board[1] && board[1] == board[2] {
return true
} else if board[0] == board[3] && board[3] == board[6] {
return true
}
}
if board[4] != 0 {
if board[1] == board[4] && board[4] == board[7] {
return true
} else if board[3] == board[4] && board[4] == board[5] {
return true
} else if board[2] == board[4] && board[4] == board[6] {
return true
} else if board[0] == board[4] && board[4] == board[8] { //
return true
}
}
if board[8] != 0 {
if board[2] == board[5] && board[5] == board[8] { //
return true
} else if board[6] == board[7] && board[7] == board[8] { //
return true
}
}
return false
}
a little bit more compact:
func checkWin(board: [Int]) -> Bool{
let checks = [
//rows
[0,1,2],
[3,4,5],
[6,7,8],
//columns
[0,3,6],
[1,4,7],
[2,5,8],
//cross
[0,4,8],
[2,4,6]
]
for check in checks{
if board[check[0]] != 0
&& board[check[0]] == board[check[1]]
&& board[check[1]] == board[check[2]]
{
return true
}
}
return false
}
better to get also the winner-id or 0 for no winner:
func getWinner(board: [Int]) -> Int{
let checks = [
//rows
[0,1,2],
[3,4,5],
[6,7,8],
//columns
[0,3,6],
[1,4,7],
[2,5,8],
//cross
[0,4,8],
[2,4,6]
]
for check in checks{
if board[check[0]] != 0
&& board[check[0]] == board[check[1]]
&& board[check[1]] == board[check[2]]
{
return board[check[0]]
}
}
return 0
}
or if you like to put it in an enum:
enum Winner {
case none
case player(id: Int)
}
func checkWin(board: [Int]) -> Winner{
let checks = [
//rows
[0,1,2],
[3,4,5],
[6,7,8],
//columns
[0,3,6],
[1,4,7],
[2,5,8],
//cross
[0,4,8],
[2,4,6]
]
for check in checks{
if board[check[0]] != 0
&& board[check[0]] == board[check[1]]
&& board[check[1]] == board[check[2]]
{
return .player(id: board[check[0]])
}
}
return .none
}

Reset Boolean values?

I am trying to get my password verifier to loop until a correct response is give. It's looping right now, but its not resetting the boolean values after the first iteration, resulting in a valid response the second time no matter which password. How do I make the loop check for every value, or reset the table every time?
boolean atLeast8 = false;
boolean oneLower = false;
boolean oneUpper = false;
boolean oneNumber = false;
boolean oneSpecial = false;
boolean noAnd = false;
boolean noEnd = false;
do
{
System.out.println("Enter your password. " + '\n');
password = input.nextLine();
System.out.println();
for(int i=0; i<password.length(); i++) //Checks for values througout the length of the string
{
char c = password.charAt(i);
if(password.length() >= 8)
atLeast8 = true;
if(Character.isLowerCase(c))
oneLower = true;
if(Character.isUpperCase(c))
oneUpper = true;
if(Character.isDigit(c))
oneNumber = true;
if(c=='!' || c=='#' || c=='#' || c=='$' || c=='%' || c=='^' || c=='&' || c=='&' || c=='*')
oneSpecial = true;
if (password.indexOf("and") < 0)
noAnd = true;
if (password.indexOf("end") < 0)
noEnd = true;
}
if(atLeast8 && oneLower && oneUpper && oneNumber && oneSpecial && noAnd && noEnd) //Does the string contain any true values?
{
System.out.println("Valid.");
}
else
{
System.out.println("Invalid!"); //Does the string contain any false values?
}
if(!atLeast8)
{
System.out.println("Must be at least 8 characters long.");
}
if(!oneLower)
{
System.out.println("Must contain one lower case letter.");
}
if(!oneUpper)
{
System.out.println("Must contain one upper case letter.");
}
if(!oneNumber)
{
System.out.println("Must contain one numeric digit.");
}
if(!oneSpecial)
{
System.out.println("Must contain one special character.");
}
if(!noAnd)
{
System.out.println("Must not contain the word 'and'.");
}
if(!noEnd)
{
System.out.println("Must not contain the word 'end'.");
}
}while (atLeast8 == false || oneLower == false || oneUpper == false || oneNumber == false || oneSpecial == false || noAnd == false || noEnd == false);
}
}
Can't you just put
atLeast8 = false;
oneLower = false;
oneUpper = false;
oneNumber = false;
oneSpecial = false;
noAnd = false;
noEnd = false;
at the start of the loop (somewhere between the "do {" and the "for")
Or, is that being too simplistic?