I have this function
bool nameExists(players, player) {
players.forEach((f) {
if (f.data['name'].toLowerCase() == player.toLowerCase()) {
return true;
}
});
return false;
}
It always return false, even if the condition is satisfied.
Any ideas?
There is no way to return a value from forEach
.
Just use a for
loop instead.
bool nameExists(players, player) {
for(var f in players) {
if (f.data['name'].toLowerCase() == player.toLowerCase()) {
return true;
}
}
return false;
}
For this specific use case, you can also use any()
instead of forEach()
, e.g.
bool nameExists(players, player) =>
players.any((f) => f.data["name"].toLowerCase() == player.toLowerCase());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With